Module: Machines::Commandline

Defined in:
lib/machines/commandline.rb

Instance Method Summary collapse

Instance Method Details

#build(options) ⇒ Object

Loads Machinesfile, opens an SCP connection and runs all commands and file uploads



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/machines/commandline.rb', line 4

def build options
  $conf.machine_name = options.shift
  $conf.task = options.shift
  init
  load_machinesfile

  task $conf.task.to_sym if $conf.task

  ssh_options = {:paranoid => false}
  if $conf.machine.cloud
    username = $conf.machine.cloud.username
    ssh_options[:keys] = [$conf.machine.cloud.private_key_path]
  else
    username = $conf.machine.user
    ssh_options[:password] = $conf.password
  end

  if $conf.log_only
    $conf.commands.each do |command|
      command.run
    end
  else
    Kernel.trap("INT") { prepare_to_exit }
    begin
      Command.ssh = Net::SSH.start $conf.machine.address, username, ssh_options
      Command.scp = Net::SCP.new(Command.ssh)
      $conf.commands.each do |command|
        command.run
        Command.file.flush
        exit if $exit_requested
      end
    ensure
      Command.ssh.close
    end
  end
end

#dryrun(options) ⇒ Object



41
42
43
44
# File 'lib/machines/commandline.rb', line 41

def dryrun options
  $conf.log_only = true
  build options
end

#execute(options) ⇒ Object

Execute a given command e.g. dryrun, build, generate, htpasswd, packages, override, tasks



47
48
49
50
51
52
53
54
55
56
# File 'lib/machines/commandline.rb', line 47

def execute(options)
  help = Help.new
  action = options.shift
  if help.actions.include?(action)
    action = 'generate' if action == 'new'
    send action, options
  else
    say help.syntax
  end
end

#generate(options) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/machines/commandline.rb', line 58

def generate options
  dir = options.first || './'
  if File.exists? dir
    confirm = ask "Overwrite '#{dir}' (y/n)? "
    return unless confirm.downcase == 'y'
  end
  FileUtils.cp_r(File.join($conf.application_dir, 'template', '/.'), dir)
  FileUtils.mkdir_p(File.join(dir, 'packages'))
  say "Project created at #{dir}/"
end

#htpasswd(options) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/machines/commandline.rb', line 69

def htpasswd options
  path = File.join($conf.webserver, 'htpasswd')
  say "Generate BasicAuth password and add to #{path}"
  username = ask('Username: ')
  password = enter_password 'users'

  crypted_pass = password.crypt(WEBrick::Utils.random_string(2))
  FileUtils.mkdir_p File.dirname(path)
  File.open(path, 'a') {|file| file.puts "#{username}:#{crypted_pass}" }
  say "Password encrypted and added to #{path}"
end

#initObject



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/machines/commandline.rb', line 81

def init
  $exit_requested = false
  $conf.passwords = []
  $conf.commands = []
  $conf.tasks = {}
  $conf.load('config.yml')

  Command.file ||= Machines::Logger.new File.open('log/output.log', 'w')
  Command.debug ||= Machines::Logger.new File.open('log/debug.log', 'w')
  Command.console ||= Machines::Logger.new STDOUT, :truncate => true
end

#load_machinesfileObject



93
94
95
96
97
98
99
100
101
# File 'lib/machines/commandline.rb', line 93

def load_machinesfile
  eval File.read('Machinesfile'), nil, "eval: Machinesfile"
rescue LoadError => e
  if e.message =~ /Machinesfile/
    raise LoadError, "Machinesfile does not exist. Use `machines new <DIR>` to create a template."
  else
    raise
  end
end

#override(package) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/machines/commandline.rb', line 116

def override package
  package = package.first
  destination = File.join('packages', "#{package}.rb")
  answer = File.exists?(destination) ? ask('Project package already exists. Overwrite? (y/n)') : 'y'
  if answer == 'y'
    source = File.join($conf.application_dir, 'packages', "#{package}.rb")
    FileUtils.cp(source, destination)
    say "Package copied to #{destination}"
  else
    say 'Aborted.'
  end
end

#packages(notused) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/machines/commandline.rb', line 103

def packages notused
  say 'Default packages'
  Dir[File.join($conf.application_dir, 'packages', '**/*.rb')].each do |package|
    say " * #{File.basename(package, '.rb')}"
  end
  say ''

  say 'Project packages'
  Dir[File.join('packages', '**/*.rb')].each do |package|
    say " * #{File.basename(package, '.rb')}"
  end
end

#tasksObject



129
130
131
132
133
134
135
136
137
# File 'lib/machines/commandline.rb', line 129

def tasks
  $conf.log_only = true
  init
  load_machinesfile
  say 'Tasks'
  $conf.tasks.each do |task_name, settings|
    say "  %-20s #{settings[:description]}" % task_name
  end
end