Class: IntercityCLI::SSHKey

Inherits:
Thor
  • Object
show all
Defined in:
lib/intercity/cli/ssh_key.rb

Instance Method Summary collapse

Instance Method Details

#addObject



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
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/intercity/cli/ssh_key.rb', line 12

def add()
  configuration = IntercityCLI::Configuration.new()
  address = options[:server] || configuration.active_server!

  node_file = File.join(configuration.nodes_directory, "#{address}.json")

  if File.exists?(node_file)
    raw_json = File.read(node_file)
    json = JSON.parse(raw_json)
  else
    raise "No configuration file for this server yet! Please create one by running:\n\n$ intercity server install <ip address>\n\n"
  end

  ssh_key_path = File.expand_path(options[:ssh_public_key_path])
  if !File.exists?(ssh_key_path)
    raise "You don't have an SSH key yet. Generate one with ssh-keygen"
  end

  json["deploy_users"] ||= []
  json["deploy_users"] << "deploy"
  json["deploy_users"].uniq!

  json["ssh_deploy_keys"] ||= []
  json["ssh_deploy_keys"] << File.read(ssh_key_path)
  json["ssh_deploy_keys"].uniq!

  File.open(node_file, "w+") do |f|
    f.write(JSON.pretty_generate(json))
  end

  server = address
  user = options[:user]
  on server do |host|
    host.user = user

    upload! node_file, "/root/server_config.json"

    execute 'chef-solo -c solo.rb -j ~/server_config.json -o "recipe[ssh_deploy_keys]"'
  end
end