Class: AmoebaDeployTools::Node

Inherits:
Command
  • Object
show all
Includes:
Concerns::SSH
Defined in:
lib/amoeba_deploy_tools/commands/node.rb

Instance Method Summary collapse

Methods included from Concerns::SSH

included, #knife_solo, #node_host_args, #ssh_run

Methods inherited from Command

#initialize

Methods included from Concerns::Hooks

included, #invoke_command

Constructor Details

This class inherits a constructor from AmoebaDeployTools::Command

Instance Method Details

#bootstrapObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/amoeba_deploy_tools/commands/node.rb', line 14

def bootstrap
  logger.info 'Starting `bootstrap`!'

  refresh
  knife_solo :prepare, 'bootstrap-version' => options[:version], ssh: true, interactive: options[:interactive]

  knife_solo :cook, ssh: true, include_private_key: true, interactive: options[:interactive] do |j|
    j.run_list = ['role[base]']
  end

  force_deployer

  pull

  logger.warn 'Node bootstrapped successfully, you can now push to the node:'
  logger.warn "\tamoeba node push --node #{options[:node]}\n"
end

#exec(cmd) ⇒ Object



118
119
120
# File 'lib/amoeba_deploy_tools/commands/node.rb', line 118

def exec(cmd)
  ssh_run(cmd, interactive: true)
end

#force_deployerObject



33
34
35
36
# File 'lib/amoeba_deploy_tools/commands/node.rb', line 33

def force_deployer
  logger.info 'Starting force_deployer'
  data_bag(:nodes)[node.name] = { deployment: { user: node.depoyment_.user || 'deploy' } }
end

#listObject



71
72
73
74
75
# File 'lib/amoeba_deploy_tools/commands/node.rb', line 71

def list
  inside_kitchen do
    puts Dir.glob('nodes/*.json').sort.map {|n| File.basename(n).sub(/\.json$/, '')}
  end
end

#pullObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/amoeba_deploy_tools/commands/node.rb', line 47

def pull
  logger.info 'Starting `pull`!'
  force_deployer unless remote_node.deployment_.user

  raw_json = ssh_run('sudo cat ~deploy/node.json', silent: true)
  # Store the remote_node databag
  data_bag(:nodes)[node.name]  = JSON.load raw_json

  # Now check and see if we are missing the private_key on this node
  private_key = remote_node.private_key || 'default'
  private_key_raw = remote_node.private_key_raw

  if private_key_raw
    # If we don't already have the private_key in our config, let's add it
    unless config.private_keys_[private_key]
      logger.info "Saving new private key `#{private_key}` to config file..."
      config.private_keys![private_key] = private_key_raw
      config.save
    end
  end

end

#pushObject



39
40
41
42
43
44
# File 'lib/amoeba_deploy_tools/commands/node.rb', line 39

def push
  logger.info 'Starting push...'
  refresh
  knife_solo :cook, ssh: true, include_private_key: true
  pull
end

#refreshObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/amoeba_deploy_tools/commands/node.rb', line 84

def refresh
  logger.info "Starting `refresh`!"
  inside_kitchen do
    # Handle authorized_keys
    logger.debug '# Refreshing authorized_keys'
    Dir.glob('authorized_keys/*') do |user_dir|
      if File.directory? user_dir
        user_name = File.basename(user_dir)
        logger.info "Processing SSH keys for user #{user_name}."
        user_bag = data_bag(:authorized_keys)[user_name]
        user_bag[:keys] = []

        Dir.glob(File.join(user_dir, '*')) do |key_file|
          logger.debug "Reading key file: #{key_file}"
          user_bag[:keys] << File.read(key_file).strip
        end

        logger.info "Writing #{user_bag.options[:filename]}"
        user_bag.save
      else
        logger.info "Ignoring file in authorized_keys (must be inside a directory): #{f}"
      end
    end

    logger.debug '# Ensuring bundle is up to date'
    # Handle bundler, ensure it's up to date
    unless system('bundle check > /dev/null 2>&1')
      logger.info "Bundle out of date! Running bundle update..."
      Cocaine::CommandLine.new('bundle', 'install').run
    end
  end
end

#sshObject



123
124
125
# File 'lib/amoeba_deploy_tools/commands/node.rb', line 123

def ssh
  exec nil
end

#sudo(cmd) ⇒ Object



128
129
130
131
# File 'lib/amoeba_deploy_tools/commands/node.rb', line 128

def sudo(cmd)
  # pull args off of cmd
  exec("sudo #{cmd}")
end