Class: Machines::Commandline

Inherits:
Object
  • Object
show all
Defined in:
lib/machines/commandline.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommandline

Returns a new instance of Commandline.



4
5
6
# File 'lib/machines/commandline.rb', line 4

def initialize
  @core = Core.new
end

Class Method Details

.execute(options) ⇒ Object

Execute a command (build dryrun generate htpasswd packages override tasks)



9
10
11
12
13
14
15
16
17
# File 'lib/machines/commandline.rb', line 9

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

Instance Method Details

#build(options) ⇒ Object

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



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/machines/commandline.rb', line 20

def build options
  $conf.machine_name = options.shift
  return say(Help.new.syntax) unless $conf.machine_name
  init
  @core.package 'Machinesfile'

  @core.task options if options.any?

  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



57
58
59
60
# File 'lib/machines/commandline.rb', line 57

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

#generate(options) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/machines/commandline.rb', line 62

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



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/machines/commandline.rb', line 73

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



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/machines/commandline.rb', line 85

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

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

#list(notused) ⇒ Object



97
98
99
# File 'lib/machines/commandline.rb', line 97

def list notused
  say Help.new.machine_list
end

#override(package) ⇒ Object



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

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



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

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

#tasks(options) ⇒ Object



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

def tasks options
  $conf.machine_name = options.shift
  return say(Help.new.syntax) unless $conf.machine_name

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