Class: Provider::ChefServer::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/providers/chefserver/instance.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Instance

Returns a new instance of Instance.



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
# File 'lib/providers/chefserver/instance.rb', line 5

def initialize(config)
  @access_key_id                                = config[:cloudprovider_access_key_id]
  @secret_access_key                            = config[:cloudprovider_secret_access_key]
  @name                                         = config[:name]
  @image                                        = config[:image]
  @security_groups                              = config[:security_groups]
  @ssh_user                                     = config[:ssh_user]
  @ssh_port                                     = config[:ssh_port]
  @identity_file                                = config[:identity_file]
  @aws_ssh_key_id                               = config[:aws_ssh_key_id]
  @environment                                  = config[:environment]
  @role                                         = config[:role]
  @roles                                        = "role[#{config[:role]}]"
  @flavor                                       = config[:flavor]
  @region                                       = config[:region]
  @availability_zone                            = config[:availability_zone]
  @verbose                                      = config[:verbose]
  Chef::Config[:knife][:image]                  = @image
  Chef::Config[:knife][:aws_ssh_key_id]         = @aws_ssh_key_id
  Chef::Config[:knife][:aws_access_key_id]      = @access_key_id
  Chef::Config[:knife][:aws_secret_access_key]  = @secret_access_key
  Chef::Config[:knife][:region]                 = @region
  Chef::Config[:knife][:availability_zone]      = @availability_zone
  Chef::Config[:knife][:log_level]              = @verbose
  @logger = Veronic::Deployer.new().logger
end

Instance Method Details

#bootstrap(recursive_count = 0) ⇒ Object



52
53
54
55
56
57
58
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
# File 'lib/providers/chefserver/instance.rb', line 52

def bootstrap(recursive_count=0)
  @logger.info "Bootstrapping ec2 server #{@name} ..."
  
  node = Chef::Knife::Ec2ServerCreate.new()

  node.config[:image]             = @image
  node.config[:flavor]            = @flavor
  node.config[:security_groups]   = @security_groups
  node.config[:ssh_user]          = @ssh_user
  node.config[:ssh_port]          = @ssh_port
  node.config[:chef_node_name]    = @name
  node.config[:identity_file]     = @identity_file
  node.config[:environment]       = @environment
  node.config[:log_level]         = @verbose
  node.config[:template_file]     = '/etc/veronic/bootstrap/lifted-chef.erb'

  @logger.info node.config
  begin
    node.run
  rescue => e
    @logger.info "Creation of #{@name} failed"
    @logger.info "Message: " + e.inspect
    @logger.info "Stacktrace:#{e.backtrace.map {|l| "  #{l}\n"}.join}"
    self.destroy([node.server.id]) if node.server
    if recursive_count < 10
      @logger.info "Creation of #{@name} retrying #{recursive_count}"
      self.bootstrap(recursive_count+=1) 
    else
      @logger.info "Creation of #{@name} failed after #{recursive_count} retry"
      exit 1
    end
  end
end

#clientObject



138
139
140
# File 'lib/providers/chefserver/instance.rb', line 138

def client
  Provider::ChefServer::Client.new(@name)
end

#createObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/providers/chefserver/instance.rb', line 32

def create
  @logger.info "Creating ec2 server #{@name} ..."
  
  node = Chef::Knife::Ec2ServerCreate.new()

  node.config[:run_list]          = [@roles]
  node.config[:image]             = @image
  node.config[:flavor]            = @flavor
  node.config[:security_groups]   = @security_groups
  node.config[:ssh_user]          = @ssh_user
  node.config[:ssh_port]          = @ssh_port
  node.config[:chef_node_name]    = @name
  node.config[:identity_file]     = @identity_file
  node.config[:environment]       = @environment
  node.config[:log_level]         = @verbose

  @logger.info node.config
  node.run
end

#delete_client_key(node, client_key = "/etc/chef/client.pem") ⇒ Object



142
143
144
145
# File 'lib/providers/chefserver/instance.rb', line 142

def delete_client_key(node, client_key="/etc/chef/client.pem")
  @logger.info "Deleting client_key #{client_key}"
  self.ssh(node, "sudo chef-client -W > /dev/null ; sudo rm -f #{client_key}", true)
end

#destroy(instance_ids = []) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/providers/chefserver/instance.rb', line 86

def destroy(instance_ids=[])
  @logger.info "Deleting ec2 server #{@name} ..."

  node = Chef::Knife::Ec2ServerDelete.new()

  node.config[:purge]           = true
  node.config[:chef_node_name]  = @name
  node.config[:yes]             = true
  node.name_args                = instance_ids

  @logger.info node.config
  node.run
  node.destroy_item(Chef::Node, @name, "node")
  node.destroy_item(Chef::ApiClient, @name, "client")
end

#set_environmentObject



108
109
110
111
112
113
114
115
# File 'lib/providers/chefserver/instance.rb', line 108

def set_environment
  node = Chef::Node.new.tap do |n|
    n.name( @name )
    n.chef_environment( @environment )
  end
  node.save
  @logger.info "Environment: #{@environment}"
end

#set_roleObject



102
103
104
105
106
# File 'lib/providers/chefserver/instance.rb', line 102

def set_role
  node = Chef::Knife::NodeRunListAdd.new()
  node.name_args = [@name, @roles]
  node.run
end

#ssh(query, cmd_line, manual) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/providers/chefserver/instance.rb', line 117

def ssh(query, cmd_line, manual)
  knife_ssh = Chef::Knife::Ssh.new()

  knife_ssh.config[:manual]         = manual
  knife_ssh.config[:ssh_user]       = @ssh_user
  knife_ssh.config[:identity_file]  = @identity_file
  knife_ssh.config[:log_level]      = @verbose
  
  unless manual
    if @environment
      query += "#{query.empty? ? '' : ' AND'} chef_environment:#{@environment}"
    end
    if @role
      query += "#{query.empty? ? '' : ' AND'} role:#{@role}"
    end
  end

  knife_ssh.name_args = [query, cmd_line]
  sys_status =  knife_ssh.run
end