Class: Propro::CLI

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/propro/cli.rb

Constant Summary collapse

INIT_TEMPLATES =
{
  db: {
    paths: %w[ vps db ],
    desc: 'backend database server'
  },
  app: {
    paths: %w[ vps app ],
    desc: 'frontend application server'
  },
  web: {
    paths: %w[ vps app db ],
    desc: 'standalone web server'
  },
  vagrant: {
    paths: %w[ vagrant ],
    desc: 'standalone Vagrant development VM'
  }
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_rootObject



26
27
28
# File 'lib/propro/cli.rb', line 26

def self.source_root
  File.join(File.dirname(__FILE__), 'cli/templates')
end

Instance Method Details

#build(input) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/propro/cli.rb', line 44

def build(input)
  infile = absolute_path(input)
  script = Script.load(input).to_bash
  if (output = options[:output])
    File.write(absolute_path(output), script)
  else
    STDOUT << script
  end
end

#deploy(script_path) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/propro/cli.rb', line 59

def deploy(script_path)
  require 'net/ssh'
  require 'net/scp'
  require 'io/console'

  puts Propro.color_banner
  puts

  script   = Script.load(script_path)
  address  = (options[:server] || script.get_server)
  password = (options[:password] || script.get_password || ask_password)
  user     = (options[:user] || script.get_user)
  remote_home = (user == 'root' ? '/root' : "/home/#{user}")
  remote_log_path    = "#{remote_home}/provision.log"
  remote_script_path = "#{remote_home}/provision.sh"
  remote_script_url  = address + remote_script_path

  say_event 'build', script_path
  script_data = StringIO.new(script.to_bash)

  raise ArgumentError, 'no server address has been provided'  if !address
  raise ArgumentError, 'no server password has been provided' if !password

  say_event 'connect', "#{user}@#{address}"
  Net::SSH.start(address, user, password: password) do |session|
    say_event 'upload', "#{script_path} -> #{remote_script_url}"
    session.scp.upload!(script_data, remote_script_path)
    session.exec!("chmod +x #{remote_script_path}")
    session.exec!("touch #{remote_log_path}")

    tail = session.exec("tail -f #{remote_log_path}") do |ch, stream, data|
      STDOUT.write(data)
      STDOUT.flush
    end

    sleep 1 # ughhhhhh
    say_event 'run', remote_script_url
    puts

    session.exec(remote_script_path) do |ch|
      ch.on_data { } # mute stdout
    end
  end
rescue IOError # uggghhhhhhhhhh
  say_event 'done', "#{address} is rebooting"
end

#init(outname = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/propro/cli.rb', line 32

def init(outname = nil)
  key      = options[:template].to_sym
  outfile  = absolute_path(outname || "#{key}.propro")
  type     = INIT_TEMPLATES[key]
  @paths   = type[:paths]
  @desc    = type[:desc]
  @sources = Package.sources_for_paths('lib', *@paths)
  template 'init.tt', outfile
end