Class: Mccloud::Provider::Core::Stack

Inherits:
Object
  • Object
show all
Defined in:
lib/mccloud/provider/aws/stack.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Stack

Returns a new instance of Stack.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mccloud/provider/aws/stack.rb', line 25

def initialize(env)
  super(env)
#      @forwardings=Array.new
   @rewrite_names=true
   @user=Hash.new
   @private_key=Hash.new
   @public_key=Hash.new
   @userdata_file=Hash.new
   @key_name=Hash.new
   @provider_options={:region => "us-east-1"}
end

Instance Attribute Details

#create_optionsObject

Returns the value of attribute create_options.



13
14
15
# File 'lib/mccloud/provider/aws/stack.rb', line 13

def create_options
  @create_options
end

#instanceObject

Returns the value of attribute instance.



15
16
17
# File 'lib/mccloud/provider/aws/stack.rb', line 15

def instance
  @instance
end

#jsonfileObject

Returns the value of attribute jsonfile.



14
15
16
# File 'lib/mccloud/provider/aws/stack.rb', line 14

def jsonfile
  @jsonfile
end

#key_nameObject

Returns the value of attribute key_name.



21
22
23
# File 'lib/mccloud/provider/aws/stack.rb', line 21

def key_name
  @key_name
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/mccloud/provider/aws/stack.rb', line 9

def name
  @name
end

#paramsObject

Returns the value of attribute params.



10
11
12
# File 'lib/mccloud/provider/aws/stack.rb', line 10

def params
  @params
end

#private_keyObject

Returns the value of attribute private_key.



19
20
21
# File 'lib/mccloud/provider/aws/stack.rb', line 19

def private_key
  @private_key
end

#providerObject

Returns the value of attribute provider.



11
12
13
# File 'lib/mccloud/provider/aws/stack.rb', line 11

def provider
  @provider
end

#provider_optionsObject

Returns the value of attribute provider_options.



12
13
14
# File 'lib/mccloud/provider/aws/stack.rb', line 12

def provider_options
  @provider_options
end

#public_keyObject

Returns the value of attribute public_key.



20
21
22
# File 'lib/mccloud/provider/aws/stack.rb', line 20

def public_key
  @public_key
end

#rewrite_namesObject

Returns the value of attribute rewrite_names.



16
17
18
# File 'lib/mccloud/provider/aws/stack.rb', line 16

def rewrite_names
  @rewrite_names
end

#userObject

Returns the value of attribute user.



18
19
20
# File 'lib/mccloud/provider/aws/stack.rb', line 18

def user
  @user
end

#userdata_fileObject

Returns the value of attribute userdata_file.



22
23
24
# File 'lib/mccloud/provider/aws/stack.rb', line 22

def userdata_file
  @userdata_file
end

Instance Method Details

#exists?Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/mccloud/provider/aws/stack.rb', line 178

def exists?
  false
end

#filtered_instance_namesObject



193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/mccloud/provider/aws/stack.rb', line 193

def filtered_instance_names
  fullname_instances=self.instance_names
  short_instances=[]
  filter=Mccloud.environment.config.mccloud.filter
  fullname_instances.each do |instancename|
    if instancename.start_with?(filter)
      short=instancename
      short[filter]=""
      short_instances << short
    end
  end
  return short_instances
end

#forward_port(name, local, remote) ⇒ Object

@this_instance=nil



235
236
237
238
# File 'lib/mccloud/provider/aws/stack.rb', line 235

def forward_port(name,local,remote)
  #forwarding=Forwarding.new(name,local,remote)
  #forwardings << forwarding
end

#instance_namesObject



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/mccloud/provider/aws/stack.rb', line 208

def instance_names
  hash=self.json_to_hash

  instances=[]
  resources=hash["Resources"]
  resources.each do  |name,resource|

          if resource["Type"]=="AWS::EC2::Instance"

            resources[name]["Properties"]["Tags"].each do |tag|

              if tag["Key"]=="Name"
                instances << tag["Value"]
              end
            end
#                instances << hash["Resources"][name]["Properties"]["Tags"]
#                      hash["Resources"][name]["Properties"]["Tags"]=[{"Value"=>"mccloud - development - patrick - drupalblub", "Key"=>"Name"}]
        else
           # env.ui.info "found something else"
          end
  end
  return instances
end

#jsonObject



164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/mccloud/provider/aws/stack.rb', line 164

def json
  begin
    json=open(@jsonfile) {|f| f.read }
    hash=JSON.parse(json)
  rescue Errno::ENOENT => e
    env.ui.info "Error getting json file -  #{e.message}"
    exit
  rescue JSON::ParserError => e
    env.ui.info "Error parsing json file -  #{e.message}"
    exit
  end
  return json
end

#json_escape(string) ⇒ Object



158
159
160
161
# File 'lib/mccloud/provider/aws/stack.rb', line 158

def json_escape(string)
  parse = JSON.parse({ 'json' => string }.to_json)
  return parse['json']
end

#json_escape_file(filename) ⇒ Object



150
151
152
153
154
# File 'lib/mccloud/provider/aws/stack.rb', line 150

def json_escape_file(filename)
  data=''
    File.open(filename) {|f| data << f.read}
    return self.json_escape(data)
end

#json_rewriteObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/mccloud/provider/aws/stack.rb', line 96

def json_rewrite
  hash=self.json_to_hash
  unknown_counter=1
  # First rewrite the Name Tags
  hash["Resources"].each do  |name,resource|

          if resource["Type"]=="AWS::EC2::Instance"
            foundName=false
            foundKeyName=false
            instance_name=""

            rewrittenTags=Array.new

             # Loop over each hash
             hash["Resources"][name]["Properties"]["Tags"].each do |tag_hash|

              new_tag_hash=tag_hash

              # If we found the Name tag rewrite it
               if tag_hash["Key"]="Name"
                 # Need to rewrite name
                 new_tag_hash={"Value"=>"#{Mccloud.environment.config.mccloud.filter}#{tag_hash["Value"]}", "Key"=>"Name"}
                 instance_name="#{tag_hash["Value"]}"
                 foundName=true
               end

               #if tag_hash["Key"]="KeyName"
              #   foundKeyName=true
               #end

               rewrittenTags << new_tag_hash

            end #Hash Tags iteration
            unless foundName
              rewrittenTags << {"Value"=>"#{Mccloud.environment.config.mccloud.filter}noname-#{unkown_counter}", "Key"=>"Name"}
              instance_name="noname-#{unkown_counter}"
              unknown_counter=unknown_counter+1
            end

            hash["Resources"][name]["Properties"]["Tags"]=rewrittenTags

            unless hash["Resources"][name]["Properties"].has_key?("UserData")
              env.ui.info "merging in Userdata file for server #{instance_name} - #{self.userdata_file_for_instance(instance_name)}"
              userdata=json_escape_file(self.userdata_file_for_instance(instance_name))
              hash["Resources"][name]["Properties"]["UserData"]={ "Fn::Base64" => "#{userdata}" }
            end

          end #EC2 Instance
  end


  return hash.to_json
end

#json_to_hashObject



182
183
184
185
186
187
188
189
190
191
# File 'lib/mccloud/provider/aws/stack.rb', line 182

def json_to_hash
  hash=nil
  begin
    hash=JSON.parse(self.json)
  rescue JSON::ParserError => e
    env.ui.info "Error parsing json file -  #{e.message}"
    exit
  end
  return hash
end

#key_name_for_instance(machinename) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/mccloud/provider/aws/stack.rb', line 64

def key_name_for_instance(machinename)
  default_key_name=key_name[:default]
  if key_name[machinename].nil?
    return default_key_name
  else
    return key_name[machinename]
  end
end

#private_key_for_instance(machinename) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/mccloud/provider/aws/stack.rb', line 37

def private_key_for_instance(machinename)
  default_private_key=private_key[:default]
  if private_key[machinename].nil?
    return default_private_key
  else
    return private_key[machinename]
  end
end

#public_key_for_instance(machinename) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/mccloud/provider/aws/stack.rb', line 46

def public_key_for_instance(machinename)
  default_public_key=public_key[:default]
  if public_key[machinename].nil?
    return default_public_key
  else
    return public_key[machinename]
  end
end

#reloadObject



232
233
234
# File 'lib/mccloud/provider/aws/stack.rb', line 232

def reload
  #@this_instance=nil
end

#user_for_instance(machinename) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/mccloud/provider/aws/stack.rb', line 55

def user_for_instance(machinename)
  default_user=user[:default]
  if user[machinename].nil?
    return default_user
  else
    return user[machinename]
  end
end

#userdata_file_for_instance(machinename) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/mccloud/provider/aws/stack.rb', line 73

def userdata_file_for_instance(machinename)
  default_userdata_file=userdata_file[:default]
  if userdata_file[machinename].nil?
    return default_userdata_file
  else
    return userdata_file[machinename]
  end
end