Class: CloudProcess

Inherits:
Object
  • Object
show all
Defined in:
lib/process/cloud/process/rules.rb,
lib/process/cloud/process/flavor.rb,
lib/process/cloud/process/images.rb,
lib/process/cloud/process/router.rb,
lib/process/cloud/process/server.rb,
lib/process/cloud/process/network.rb,
lib/process/cloud/process/keypairs.rb,
lib/process/cloud/process/public_ip.rb,
lib/process/cloud/process/connection.rb,
lib/process/cloud/process/subnetwork.rb,
lib/process/cloud/process/security_groups.rb,
lib/process/cloud/process/external_network.rb,
lib/process/cloud/process/security_groups.rb,
lib/process/cloud/process/subnetwork.rb,
lib/process/cloud/process/public_ip.rb,
lib/process/cloud/process/keypairs.rb,
lib/process/cloud/process/keypairs.rb,
lib/process/cloud/process/network.rb,
lib/process/cloud/process/server.rb,
lib/process/cloud/process/router.rb,
lib/process/cloud/process/rules.rb

Overview

SecurityGroups rules management

Instance Method Summary collapse

Instance Method Details

#_check_key_file(key_path, key_basename, extensions) ⇒ Object



281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/process/cloud/process/keypairs.rb', line 281

def _check_key_file(key_path, key_basename, extensions)
  found_ext = nil
  files = []
  extensions.each do |ext|
    temp_file = File.join(key_path, key_basename + ext)
    if File.exist?(temp_file) && !File.directory?(temp_file)
      found_ext = ext
      files << temp_file
    end
  end
  [found_ext, files]
end

#_get_router(router_name, sub_net_obj, hParams) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/process/cloud/process/router.rb', line 38

def _get_router(router_name, sub_net_obj, hParams)
  router_port = get_router_interface_attached(:port, hParams)

  if router_port.nil? || router_port.length == 0
    # Trying to get router
    router = get_router(router_name)
    router = create_router(router_name) if router.empty?
    create_router_interface(sub_net_obj, router) if router
  else
    router = query_router_from_port(router_port[0], hParams)
  end
  router
end

#add_ssh_user(images) ⇒ Object



61
62
63
64
65
# File 'lib/process/cloud/process/images.rb', line 61

def add_ssh_user(images)
  images.each do |image|
    image[:ssh_user] = ssh_user(image[:name])
  end
end

#assign_address(sCloudObj, hParams) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/process/cloud/process/public_ip.rb', line 90

def assign_address(sCloudObj, hParams)
  name = hParams[:server, :name]
  begin
    PrcLib.state('Getting public IP for server %s', name)
    ip_address = controller_create(sCloudObj)
    if ip_address.empty?
      PrcLib.error("Unable to assign a public IP to server '%s'", name)
      return ip_address
    end
    PrcLib.info("Public IP '%s' for server '%s' "\
                'assigned.', ip_address[:public_ip], name)
 rescue => e
   PrcLib.fatal(1, "Unable to assign a public IP to server '%s'", name, e)
  end
  ip_address
end

#coherent_keypair?(loc_kpair, keypair) ⇒ Boolean

Check if 2 keypair objects are coherent (Same public key) Parameters:

  • loc_kpair : Keypair structure representing local files existence.

    see keypair_detect
    
  • keypair : Keypair object to check.

return:

  • coherent : Boolean. True if same public key.

Returns:

  • (Boolean)


324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/process/cloud/process/keypairs.rb', line 324

def coherent_keypair?(loc_kpair, keypair)
  # send keypairs by parameter
  is_coherent = false

  pub_keypair = keypair[:public_key]

  # Check the public key with the one found here, locally.
  if !pub_keypair.nil? && pub_keypair != ''
    return false unless loc_kpair[:public_key_exist?]
    begin
      loc_pubkey = File.read(File.join(loc_kpair[:keypair_path],
                                       loc_kpair[:public_key_name]))
   rescue => e
     PrcLib.error("Unable to read '%s'.\n%s",
                  loc_kpair[:public_key_file], e.message)
    else
      if loc_pubkey.split(' ')[1].strip == pub_keypair.split(' ')[1].strip
        is_coherent = true
      end
    end
  else
    PrcLib.warning('Unable to verify keypair coherence with your local '\
                   'SSH keys. No public key (:public_key) provided.')
  end
  is_coherent
end

#connect(sCloudObj, hParams) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/process/cloud/process/connection.rb', line 24

def connect(sCloudObj, hParams)
  ssl_error_obj = SSLErrorMgt.new # Retry object
  PrcLib.debug("%s:%s Connecting to '%s' - Project '%s'",
               self.class, sCloudObj, config[:provider],
               hParams['credentials#tenant'])
  begin
    controller_connect(sCloudObj)
 rescue => e
   retry unless ssl_error_obj.error_detected(e.message, e.backtrace, e)

   PrcLib.error('%s:%s: Unable to connect.\n%s',
                self.class, sCloudObj, e.message)
   nil
  end
end

#create_keypair(sCloudObj, hParams) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/process/cloud/process/keypairs.rb', line 220

def create_keypair(sCloudObj, hParams)
  key_name = hParams['credentials#keypair_name']
  PrcLib.state("Importing keypair '%s'", key_name)
  ssl_error_obj = SSLErrorMgt.new
  begin
    keypair = controller_create(sCloudObj, hParams)
    PrcLib.info("Keypair '%s' imported.", keypair[:name])
  rescue StandardError => e
    retry unless ssl_error_obj.error_detected(e.message, e.backtrace, e)
    PrcLib.error "error importing keypair '%s'", key_name
  end
  keypair
end

#create_network(sCloudObj, hParams) ⇒ Object

Network creation It returns: nil or Provider Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/process/cloud/process/network.rb', line 84

def create_network(sCloudObj, hParams)
  name = hParams[:network_name]
  begin
    PrcLib.state("Creating network '%s'", name)
    network = controller_create(sCloudObj)
    PrcLib.info("Network '%s' created", network[:name])
 rescue => e
   PrcLib.fatal(1, "Unable to create network '%s'", name, e)
  end
  network
end

#create_router(router_name, oExternalNetwork = nil) ⇒ Object



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
# File 'lib/process/cloud/process/router.rb', line 115

def create_router(router_name, oExternalNetwork = nil)
  begin
    if oExternalNetwork
      ext_net = get_data(oExternalNetwork, :name)
      PrcLib.state("Creating router '%s' attached to the external "\
                   "Network '%s'", router_name, ext_net)
      config[:external_gateway_id] = get_data(oExternalNetwork, :id)
    else
      PrcLib.state("Creating router '%s' without external Network",
                   router_name)
    end

    router = controller_create(:router, :router_name => router_name)
    if oExternalNetwork
      PrcLib.info("Router '%s' created and attached to the external "\
                  "Network '%s'.", router_name, ext_net)
    else
      PrcLib.info("Router '%s' created without external Network.",
                  router_name)
    end
 rescue => e
   PrcLib.error "Unable to create '%s' router\n%s\n%s", router_name,
                e.message, e.backtrace.join("\n")
  end
  router
end

#create_router_interface(oSubnet, router_obj) ⇒ Object

TODO: Move router interface management to hpcloud controller. Router interface to connect to the network



169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/process/cloud/process/router.rb', line 169

def create_router_interface(oSubnet, router_obj)
  PrcLib.state("Attaching subnet '%s' to router '%s'",
               oSubnet[:name], router_obj[:name])
  begin
    controller_create(:router_interface)

  #################
  # provider_add_interface()
  # router_obj.add_interface(oSubnet.id, nil)
  rescue => e
    PrcLib.error("%s\n%s", e.message, e.backtrace.join("\n"))
  end
end

#create_rule(sCloudObj, hParams) ⇒ Object

Rules internal # —————-#



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/process/cloud/process/rules.rb', line 104

def create_rule(sCloudObj, hParams)
  rule_msg = format('%s %s:%s - %s to %s',
                    hParams[:dir], hParams[:rule_proto],
                    hParams[:port_min], hParams[:port_max],
                    hParams[:addr_map])
  PrcLib.state("Creating rule '%s'", rule_msg)
  ssl_error_obj = SSLErrorMgt.new
  begin
    rule = controller_create(sCloudObj)
    PrcLib.info("Rule '%s' created.", rule_msg)
  rescue StandardError => e
    retry unless ssl_error_obj.error_detected(e.message, e.backtrace, e)
    PrcLib.error 'error creating the rule "%s"', rule_msg
  end
  rule
end

#create_security_group(sCloudObj, hParams) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/process/cloud/process/security_groups.rb', line 122

def create_security_group(sCloudObj, hParams)
  PrcLib.state("Creating security group '%s'", hParams[:security_group])
  begin
    sg = controller_create(sCloudObj)
    PrcLib.info("Security group '%s' created.", sg[:name])
  rescue => e
    PrcLib.error("%s\n%s", e.message, e.backtrace.join("\n"))
  end
  sg
end

#create_server(sCloudObj, hParams) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/process/cloud/process/server.rb', line 124

def create_server(sCloudObj, hParams)
  name = hParams[:server_name]
  begin
    PrcLib.info('boot: meta-data provided.') if hParams[:meta_data]
    PrcLib.info('boot: user-data provided.') if hParams[:user_data]
    PrcLib.state('creating server %s', name)
    server = controller_create(sCloudObj)
    PrcLib.info("%s '%s' created.", sCloudObj, name)
  rescue => e
    PrcLib.fatal(1, "Unable to create server '%s'", name, e)
  end
  server
end

#create_subnet(sCloudObj, hParams) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/process/cloud/process/subnetwork.rb', line 55

def create_subnet(sCloudObj, hParams)
  name = hParams[:subnetwork_name]
  PrcLib.state("Creating subnet '%s'", name)
  begin
    subnet = controller_create(sCloudObj, hParams)
    PrcLib.info("Subnet '%s' created.", subnet[:name])
 rescue => e
   PrcLib.fatal(1, "Unable to create '%s' subnet.", name, e)
  end
  subnet
end

#delete_router(net_conn_obj, router_obj) ⇒ Object



142
143
144
145
146
147
148
149
150
151
# File 'lib/process/cloud/process/router.rb', line 142

def delete_router(net_conn_obj, router_obj)
  PrcLib.state("Deleting router '%s'", router.name)
  begin
    #################
    provider_delete_router(net_conn_obj, router_obj)
 # net_conn_obj.routers.get(router.id).destroy
 rescue => e
   PrcLib.error("Unable to delete '%s' router ID", router_id, e)
  end
end

#delete_router_interface(oSubnet, router_obj) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
# File 'lib/process/cloud/process/router.rb', line 183

def delete_router_interface(oSubnet, router_obj)
  PrcLib.state("Removing subnet '%s' from router '%s'",
               oSubnet.name, router_obj.name)
  subnet_id = oSubnet.id
  begin
    #################
    router_obj.remove_interface(subnet_id)
  rescue => e
    PrcLib.error("%s\n%s", e.message, e.backtrace.join("\n"))
  end
end

#delete_subnetObject



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/process/cloud/process/subnetwork.rb', line 67

def delete_subnet
  net_conn_obj = get_cloudObj(:network_connection)
  sub_net_obj = get_cloudObj(:subnetwork)

  PrcLib.state("Deleting subnet '%s'", sub_net_obj.name)
  begin
    provider_delete_subnetwork(net_conn_obj, sub_net_obj)
    net_conn_obj.subnets.get(sub_net_obj.id).destroy
 rescue => e
   PrcLib.error("%s\n%s", e.message, e.backtrace.join("\n"))
  end
end

#find_network(sCloudObj, hParams) ⇒ Object

Search for a network from his name. Name may be unique in project context, but not in the cloud system It returns: nil or Provider Object



100
101
102
103
104
105
106
# File 'lib/process/cloud/process/network.rb', line 100

def find_network(sCloudObj, hParams)
  query = { :name => hParams[:network_name] }

  query_single(sCloudObj, query, hParams[:network_name])
 rescue => e
 PrcLib.error("%s\n%s", e.message, e.backtrace.join("\n"))
end

#forj_delete_network(sCloudObj, hParams) ⇒ Object

Process Delete handler



47
48
49
50
51
# File 'lib/process/cloud/process/network.rb', line 47

def forj_delete_network(sCloudObj, hParams)
  oProvider.delete(sCloudObj, hParams)
 rescue => e
 PrcLib.error("%s\n%s", e.message, e.backtrace.join("\n"))
end

#forj_delete_rule(sCloudObj, _hParams) ⇒ Object

Process Delete handler



24
25
26
27
28
29
30
31
# File 'lib/process/cloud/process/rules.rb', line 24

def forj_delete_rule(sCloudObj, _hParams)
  ssl_error_obj = SSLErrorMgt.new
  begin
    controller_delete(sCloudObj)
  rescue => e
    retry unless ssl_error_obj.error_detected(e.message, e.backtrace, e)
  end
end

#forj_delete_server(sCloudObj, hParams) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/process/cloud/process/server.rb', line 40

def forj_delete_server(sCloudObj, hParams)
  ssl_error_obj = SSLErrorMgt.new
  begin
    controller_delete(sCloudObj)
    PrcLib.info('Server %s was destroyed ', hParams[:server][:name])
  rescue => e
    retry unless ssl_error_obj.error_detected(e.message, e.backtrace, e)
  end
end

#forj_delete_sg(sCloudObj, _hParams) ⇒ Object

Process Delete handler



74
75
76
77
78
79
80
81
# File 'lib/process/cloud/process/security_groups.rb', line 74

def forj_delete_sg(sCloudObj, _hParams)
  ssl_error_obj = SSLErrorMgt.new
  begin
    controller_delete(sCloudObj)
  rescue => e
    retry unless ssl_error_obj.error_detected(e.message, e.backtrace, e)
  end
end

#forj_get_image(sCloudObj, sId, _hParams) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/process/cloud/process/images.rb', line 73

def forj_get_image(sCloudObj, sId, _hParams)
  ssl_error_obj = SSLErrorMgt.new
  begin
    image = controller_get(sCloudObj, sId)
  rescue => e
    retry unless ssl_error_obj.error_detected(e.message, e.backtrace, e)
  end
  add_ssh_user([image])
  image
end

#forj_get_keypair(sCloudObj, keypair_name, hParams) ⇒ Object

Get cloud keypair and check coherence with local files of same name in forj files



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/process/cloud/process/keypairs.rb', line 87

def forj_get_keypair(sCloudObj, keypair_name, hParams)
  ssl_error_obj = SSLErrorMgt.new
  begin
    keypair = controller_get(sCloudObj, keypair_name)
  rescue => e
    retry unless ssl_error_obj.error_detected(e.message, e.backtrace, e)
  end

  return keypair unless hParams.exist?(:keypair_path)
  keypair_path = File.expand_path(hParams[:keypair_path])
  loc_kpair = keypair_detect(keypair_name, keypair_path, keypair_name)
  keypair_files_detected(keypair, loc_kpair) unless keypair.empty?
  keypair
end

#forj_get_network(sCloudObj, sID, hParams) ⇒ Object



53
54
55
56
57
# File 'lib/process/cloud/process/network.rb', line 53

def forj_get_network(sCloudObj, sID, hParams)
  oProvider.get(sCloudObj, sID, hParams)
rescue => e
  PrcLib.error("%s\n%s", e.message, e.backtrace.join("\n"))
end

#forj_get_or_assign_public_address(sCloudObj, hParams) ⇒ Object

Process Handler functions



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/process/cloud/process/public_ip.rb', line 24

def forj_get_or_assign_public_address(sCloudObj, hParams)
  # Function which to assign a public IP address to a server.
  server_name = hParams[:server, :name]

  PrcLib.state("Searching public IP for server '%s'", server_name)
  addresses = controller_query(sCloudObj, :server_id => hParams[:server, :id])
  if addresses.length == 0
    assign_address(sCloudObj, hParams)
  else
    addresses[0]
  end
end

#forj_get_or_create_ext_net(sCloudObj, hParams) ⇒ Object



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
56
57
# File 'lib/process/cloud/process/external_network.rb', line 23

def forj_get_or_create_ext_net(sCloudObj, hParams)
  PrcLib.state("Checking router '%s' gateway", hParams[:router, :name])

  router_obj = hParams[:router]
  router_name = hParams[:router, :name]
  network_id = hParams[:router, :gateway_network_id]
  if network_id
    external_network = forj_query_external_network(sCloudObj,
                                                   { :id => network_id },
                                                   hParams)
    PrcLib.info("Router '%s' is attached to the "\
                "external gateway '%s'.", router_name,
                external_network[:name])
  else
    PrcLib.info("Router '%s' needs to be attached to an "\
                'external gateway.', router_name)
    PrcLib.state('Attaching')
    external_network = forj_query_external_network(:network, {}, hParams)
    if !external_network.empty?
      router_obj[:gateway_network_id] = external_network[:id]
      controller_update(:router, router_obj)
      PrcLib.info("Router '%s' attached to the "\
                  "external network '%s'.",
                  router_name, external_network[:name])
    else
      PrcLib.fatal(1, "Unable to attach router '%s' to an external gateway. "\
                      'Required for boxes to get internet access. ',
                   get_data(:router, :name))
    end
  end

  # Need to keep the :network object as :external_network object type.
  external_network.type = sCloudObj
  external_network
end

#forj_get_or_create_flavor(sCloudObj, hParams) ⇒ Object

Depending on clouds/rights, we can create flavor or not. Usually, flavor records already exists, and the controller may map them CloudProcess predefines some values. Consult CloudProcess.rb for details



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/process/cloud/process/flavor.rb', line 26

def forj_get_or_create_flavor(sCloudObj, hParams)
  flavor_name = hParams['server#flavor_name']
  PrcLib.state("Searching for flavor '%s'", flavor_name)

  flavors = query_flavor(sCloudObj, { :name => flavor_name }, hParams)
  if flavors.length == 0
    if !hParams[:create]
      PrcLib.error("Unable to create %s '%s'. Creation is not "\
                   'supported.', sCloudObj, flavor_name)
      ForjLib::Data.new.set(nil, sCloudObj)
    else
      create_flavor(sCloudObj, hParams)
    end
  else
    flavors[0]
  end
end

#forj_get_or_create_image(sCloudObj, hParams) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/process/cloud/process/images.rb', line 23

def forj_get_or_create_image(sCloudObj, hParams)
  image_name = hParams['server#image_name']
  PrcLib.state("Searching for image '%s'", image_name)

  search_the_image(sCloudObj, { :name => image_name }, hParams)
  # No creation possible.
end

#forj_get_or_create_keypair(sCloudObj, hParams) ⇒ Object

KeyPair Create Process Handler The process implemented is:

  • Check local SSH keypairs with given ‘:keypair_name’

  • Check remote keypair existence

  • Compare and warn if needed.

  • Import public key found if missing remotely and name it.

Return:

  • keypair : Lorj::Data keypair object. Following additional data should be

    found in the keypair attributes
    
    • :coherent : Boolean. True, if the local keypair (public AND

      private) is coherent with remote keypair found in
      the cloud
      
    • :private_key_file: String. Path to local private key file

    • :public_key_file : String. Path to local public key file

    • :public_key : String. Public key content. (config is

      also set - Used to import it)
      


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/process/cloud/process/keypairs.rb', line 41

def forj_get_or_create_keypair(sCloudObj, hParams)
  keypair_name = hParams['credentials#keypair_name']

  PrcLib.state("Searching for keypair '%s'", keypair_name)

  keypair = forj_get_keypair(sCloudObj, keypair_name, hParams)
  if keypair.empty? &&
     hParams.exist?(:keypair_path) && hParams.exist?(:keypair_base)

    loc_kpair = keypair_detect(keypair_name, hParams[:keypair_path],
                               hParams[:keypair_base])
    keypair = keypair_import(hParams, loc_kpair)
  else
    if keypair.empty?
      PrcLib.warning("keypair '%s' was not found.", keypair_name)
    else
      keypair_display(keypair)
    end
  end
  keypair
end

#forj_get_or_create_network(sCloudObj, hParams) ⇒ Object

Process Create handler



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/process/cloud/process/network.rb', line 24

def forj_get_or_create_network(sCloudObj, hParams)
  PrcLib.state("Searching for network '%s'", hParams[:network_name])
  networks = find_network(sCloudObj, hParams)
  if networks.length == 0
    network = create_network(sCloudObj, hParams)
  else
    network = networks[0]
  end
  register(network)

  # Attaching if missing the subnet.
  # Creates an object subnet, attached to the network.
  params = {}
  unless hParams[:subnetwork_name]
    params[:subnetwork_name] = 'sub-' + hParams[:network_name]
  end

  process_create(:subnetwork, params)

  network
end

#forj_get_or_create_router(_sCloudObj, hParams) ⇒ Object

Process Create handler



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/process/cloud/process/router.rb', line 26

def forj_get_or_create_router(_sCloudObj, hParams)
  sub_net_obj = hParams[:subnetwork]

  if hParams[:router_name].nil?
    router_name = format('router-%s', hParams[:network, :name])
  else
    router_name = hParams[:router_name]
  end

  _get_router(router_name, sub_net_obj, hParams)
end

#forj_get_or_create_rule(sCloudObj, hParams) ⇒ Object

Process Create handler



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/process/cloud/process/rules.rb', line 54

def forj_get_or_create_rule(sCloudObj, hParams)
  query = {
    :dir => hParams[:dir],
    :proto => hParams[:proto],
    :port_min => hParams[:port_min],
    :port_max => hParams[:port_max],
    :addr_map => hParams[:addr_map],
    :sg_id => hParams[:sg_id]
  }
  rules = forj_query_rule(sCloudObj, query, hParams)
  if rules.length == 0
    create_rule(sCloudObj, hParams)
  else
    rules[0]
  end
end

#forj_get_or_create_server(sCloudObj, hParams) ⇒ Object

Process Handler functions



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/process/cloud/process/server.rb', line 26

def forj_get_or_create_server(sCloudObj, hParams)
  server_name = hParams[:server_name]
  PrcLib.state("Searching for server '%s'", server_name)

  config[:search_for] = server_name
  servers = forj_query_server(sCloudObj, { :name => server_name }, hParams)
  if servers.length > 0
    # Get server details
    forj_get_server(sCloudObj, servers[0][:attrs][:id], hParams)
  else
    create_server(sCloudObj, hParams)
  end
end

#forj_get_or_create_sg(sCloudObj, hParams) ⇒ Object

Process Create handler



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/process/cloud/process/security_groups.rb', line 24

def forj_get_or_create_sg(sCloudObj, hParams)
  sg_name = hParams[:security_group]
  PrcLib.state("Searching for security group '%s'", sg_name)

  security_group = forj_query_sg(sCloudObj, { :name => sg_name }, hParams)
  security_group = create_security_group(sCloudObj,
                                         hParams) unless security_group
  register(security_group)

  PrcLib.info('Configuring Security Group \'%s\'', sg_name)
  if hParams.exist?('server#ports')
    ports = hParams['server#ports']
  else # case kept for compatibility.
    ports = config[:ports]
  end

  return security_group unless ports.is_a?(Array)

  forj_sg_update_ports(ports)

  security_group
end

#forj_get_or_create_subnetwork(sCloudObj, hParams) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/process/cloud/process/subnetwork.rb', line 23

def forj_get_or_create_subnetwork(sCloudObj, hParams)
  subnets = query_subnet(sCloudObj, hParams)
  unless subnets.length == 0
    register(subnets[0])
    return subnets[0]
  end

  # Create the subnet
  subnet = create_subnet(sCloudObj, hParams)

  return nil if subnet.nil?
  register(subnet)
  subnet
end

#forj_get_public_address(sCloudObj, sId, _hParams) ⇒ Object

Function to get the IP address



59
60
61
62
63
64
65
66
# File 'lib/process/cloud/process/public_ip.rb', line 59

def forj_get_public_address(sCloudObj, sId, _hParams)
  ssl_error_obj = SSLErrorMgt.new
  begin
    controller_get(sCloudObj, sId)
  rescue => e
    retry unless ssl_error_obj.error_detected(e.message, e.backtrace, e)
  end
end

#forj_get_server(sCloudObj, sId, _hParams) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/process/cloud/process/server.rb', line 60

def forj_get_server(sCloudObj, sId, _hParams)
  ssl_error_obj = SSLErrorMgt.new
  begin
    controller_get(sCloudObj, sId)
  rescue => e
    retry unless ssl_error_obj.error_detected(e.message, e.backtrace, e)
  end
end

#forj_get_server_log(sCloudObj, sId, _hParams) ⇒ Object



138
139
140
141
142
143
144
145
# File 'lib/process/cloud/process/server.rb', line 138

def forj_get_server_log(sCloudObj, sId, _hParams)
  ssl_error_obj = SSLErrorMgt.new
  begin
    controller_get(sCloudObj, sId)
  rescue => e
    retry unless ssl_error_obj.error_detected(e.message, e.backtrace, e)
  end
end

#forj_query_external_network(_sCloudObj, sQuery, _hParams) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/process/cloud/process/external_network.rb', line 59

def forj_query_external_network(_sCloudObj, sQuery, _hParams)
  PrcLib.state('Identifying External gateway')
  begin
    # Searching for external network
    query = sQuery.merge(:external => true)
    info = {
      :notfound => 'No external network found',
      :checkmatch => 'Found 1 %s. Checking if it is an %s.',
      :nomatch => 'No %s identified as %s match',
      :found => "Found external %s '%s'.",
      :more => 'Found several %s. Searching for the first one to be an %s.'
    }
    networks = query_single(:network, query, 'external network', info)
    return Lorj::Data.new if networks.length == 0
    networks[0]
  rescue => e
    PrcLib.error("%s\n%s", e.message, e.backtrace.join("\n"))
  end
end

#forj_query_flavor(sCloudObj, sQuery, _hParams) ⇒ Object

Should return 1 or 0 flavor.



53
54
55
56
57
58
59
60
61
# File 'lib/process/cloud/process/flavor.rb', line 53

def forj_query_flavor(sCloudObj, sQuery, _hParams)
  ssl_error_obj = SSLErrorMgt.new
  begin
    list = controller_query(sCloudObj, sQuery)
  rescue => e
    retry unless ssl_error_obj.error_detected(e.message, e.backtrace, e)
  end
  list
end

#forj_query_image(sCloudObj, sQuery, _hParams) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/process/cloud/process/images.rb', line 50

def forj_query_image(sCloudObj, sQuery, _hParams)
  ssl_error_obj = SSLErrorMgt.new
  begin
    images = controller_query(sCloudObj, sQuery)
  rescue => e
    retry unless ssl_error_obj.error_detected(e.message, e.backtrace, e)
  end
  add_ssh_user(images)
  images
end

#forj_query_keypairs(sCloudObj, sQuery, hParams) ⇒ Object

Query cloud keypairs and check coherence with local files of same name in forj files located by :keypair_path



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/process/cloud/process/keypairs.rb', line 65

def forj_query_keypairs(sCloudObj, sQuery, hParams)
  ssl_error_obj = SSLErrorMgt.new
  begin
    keypairs = controller_query(sCloudObj, sQuery)
  rescue => e
    retry unless ssl_error_obj.error_detected(e.message, e.backtrace, e)
  end
  return keypairs unless hParams.exist?(:keypair_path) &&
                         hParams.exist?(:keypair_base)
  # Looping on keypairs to identify if they have a valid local ssh key.
  keypair_path = File.expand_path(hParams[:keypair_path])
  keypair_base = File.expand_path(hParams[:keypair_base])

  keypairs.each do |keypair|
    loc_kpair = keypair_detect(keypair_name, keypair_path, keypair_base)
    keypair_files_detected(keypair, loc_kpair)
  end
  keypairs
end

#forj_query_public_address(sCloudObj, sQuery, hParams) ⇒ Object

Function to query the list of addresses for one server



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/process/cloud/process/public_ip.rb', line 38

def forj_query_public_address(sCloudObj, sQuery, hParams)
  server_name = hParams[:server, :name]
  ssl_error_obj = SSLErrorMgt.new
  begin
    info = {
      :notfound => "No %s for '%s' found",
      :checkmatch => "Found 1 %s. checking exact match for server '%s'.",
      :nomatch => "No %s for '%s' match",
      :found => "Found %s '%s' for #{server_name}.",
      :more => "Found several %s. Searching for '%s'.",
      :items => :public_ip
    }
    #  list = controller_query(sCloudObj, sQuery)
    #  query_single(sCloudObj, list, sQuery, server_name, info)
    query_single(sCloudObj, sQuery, server_name, info)
  rescue => e
    retry unless ssl_error_obj.error_detected(e.message, e.backtrace, e)
  end
end

#forj_query_rule(sCloudObj, sQuery, hParams) ⇒ Object

Process Query handler



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/process/cloud/process/rules.rb', line 34

def forj_query_rule(sCloudObj, sQuery, hParams)
  rule = format('%s %s:%s - %s to %s', hParams[:dir], hParams[:rule_proto],
                hParams[:port_min], hParams[:port_max],
                hParams[:addr_map])
  PrcLib.state("Searching for rule '%s'", rule)
  ssl_error_obj = SSLErrorMgt.new
  begin
    info = {
      :items => [:dir, :proto, :port_min, :port_max, :addr_map],
      :items_form => '%s %s:%s - %s to %s'
    }
    #  list = controller_query(sCloudObj, sQuery)
    #  query_single(sCloudObj, list, sQuery, rule, info)
    query_single(sCloudObj, sQuery, rule, info)
  rescue => e
    retry unless ssl_error_obj.error_detected(e.message, e.backtrace, e)
  end
end

#forj_query_server(sCloudObj, sQuery, _hParams) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/process/cloud/process/server.rb', line 50

def forj_query_server(sCloudObj, sQuery, _hParams)
  ssl_error_obj = SSLErrorMgt.new
  begin
    controller_query(sCloudObj, sQuery)
    #  query_single(sCloudObj, sQuery, config[:search_for])
  rescue => e
    retry unless ssl_error_obj.error_detected(e.message, e.backtrace, e)
  end
end

#forj_query_sg(sCloudObj, sQuery, hParams) ⇒ Object

Process Query handler



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/process/cloud/process/security_groups.rb', line 84

def forj_query_sg(sCloudObj, sQuery, hParams)
  ssl_error_obj = SSLErrorMgt.new

  begin
    sgroups = controller_query(sCloudObj, sQuery)
  rescue => e
    retry unless ssl_error_obj.error_detected(e.message, e.backtrace, e)
    PrcLib.fatal(1, 'Unable to get list of security groups.', e)
  end
  case sgroups.length
  when 0
    PrcLib.info("No security group '%s' found", hParams[:security_group])
    nil
  when 1
    PrcLib.info("Found security group '%s'", sgroups[0, :name])
    sgroups[0]
  end
end

#forj_sg_update_ports(ports) ⇒ Object

forj get or create port on SG



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/process/cloud/process/security_groups.rb', line 48

def forj_sg_update_ports(ports)
  ports.each do |port|
    port = port.to_s if port.class != String
    if !(/^\d+(-\d+)?$/ =~ port)
      PrcLib.error("Port '%s' is not valid. Must be <Port> or "\
                   '<PortMin>-<PortMax>', port)
    else
      port_found_match = /^(\d+)(-(\d+))?$/.match(port)
      portmin = port_found_match[1]
      portmax = (port_found_match[3]) ? (port_found_match[3]) : (portmin)
      # Need to set runtime data to get or if missing
      # create the required rule.
      params = {}
      params[:dir]        = :IN
      params[:proto] = 'tcp'
      params[:port_min]   = portmin.to_i
      params[:port_max]   = portmax.to_i
      params[:addr_map]   = '0.0.0.0/0'

      # object.Create(:rule)
      process_create(:rule, params)
    end
  end
end

#get_gateway(net_conn_obj, name) ⇒ Object

Gateway management



229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/process/cloud/process/router.rb', line 229

def get_gateway(net_conn_obj, name)
  return nil if !name || !net_conn_obj

  PrcLib.state("Getting gateway '%s'", name)
  networks = net_conn_obj
  begin
    netty = networks.get(name)
 rescue => e
   PrcLib.error("%s\n%s", e.message, e.backtrace.join("\n"))
  end
  PrcLib.state("Found gateway '%s'", name) if netty
  PrcLib.state("Unable to find gateway '%s'", name) unless netty
  netty
end

#get_keypairs_path(hParams, hKeys) ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/process/cloud/process/keypairs.rb', line 294

def get_keypairs_path(hParams, hKeys)
  keypair_name = hParams['credentials#keypair_name']

  if hKeys[:private_key_exist?]
    hParams[:private_key_file] = File.join(hKeys[:keypair_path],
                                           hKeys[:private_key_name])
    PrcLib.info("Openssh private key file '%s' exists.",
                hParams[:private_key_file])
  end
  if hKeys[:public_key_exist?]
    hParams[:public_key_file] = File.join(hKeys[:keypair_path],
                                          hKeys[:public_key_name])
  else
    PrcLib.fatal(1, 'Public key file is not found. Please run'\
                    " 'forj setup %s'", config[:account_name])
  end

  PrcLib.state("Searching for keypair '%s'", keypair_name)

  hParams
end

#get_router(name) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/process/cloud/process/router.rb', line 103

def get_router(name)
  PrcLib.state("Searching for router '%s'", name)
  begin
    query = { :name => name }
    routers = query_single(:router, query, name)
    return Lorj::Data.new if routers.length == 0
    register(routers[0])
  rescue => e
    PrcLib.error("%s\n%s", e.message, e.backtrace.join("\n"))
  end
end

#get_router_interface_attached(sCloudObj, hParams) ⇒ Object

Function to get the router ID in the network from the list of routers found. Query ports devices.



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/process/cloud/process/router.rb', line 198

def get_router_interface_attached(sCloudObj, hParams)
  name = hParams[:network, :name]
  PrcLib.state("Searching for router port attached to the network '%s'", name)
  routers = controller_query(:router, {})
  routers.each do |router|
    begin
      router_name = router[:name]
      # Searching for router port attached
      #################
      query = { :network_id => hParams[:network, :id],
                :device_id => router[:id] }
      info = {
        :notfound => "Network '#{name}' not attached to router "\
                     "'#{router_name}'",
        :checkmatch => 'Found 1 router %s. '\
                             "Checking exact match for network '%s'.",
        :nomatch => "No router %s for network '%s' match",
        :found => "Found router %s ID (#{router_name}) %s attached to "\
                  "network '#{name}'.",
        :more => "Found several router %s. Searching for network '%s'.",
        :items => [:id]
      }
      interfaces = query_single(sCloudObj, query, name, info)
      return interfaces unless interfaces.length == 0
    rescue => e
      PrcLib.error("%s\n%s", e.message, e.backtrace.join("\n"))
    end
  end
end

#keypair_detect(keypair_name, key_fullpath, key_basename = nil) ⇒ Object

Build keypair data information structure with files found in local filesystem. Take care of priv with or without .pem and pubkey with pub. :keypair_path data settings is changing to become just a path to the keypair files, the base keypair. Which will introduce a :keypair_base in the setup.



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/process/cloud/process/keypairs.rb', line 240

def keypair_detect(keypair_name, key_fullpath, key_basename = nil)
  # When uses key_basename, we switch to the new model
  # using :keypair_path and :keypair_base in setup
  if key_basename.nil?
    key_basename = File.basename(key_fullpath)
    key_path = File.expand_path(File.dirname(key_fullpath))
  else
    key_path = File.expand_path(key_fullpath)
  end

  obj_match = key_basename.match(/^(.*?)(\.pem|\.pub)?$/)
  key_basename = obj_match[1]

  private_key_ext, files = _check_key_file(key_path, key_basename,
                                           ['', '.pem'])

  if private_key_ext
    priv_key_exist = true
    priv_key_name = key_basename + private_key_ext
  else
    files.each do |temp_file|
      PrcLib.warning('keypair_detect: Private key file name detection has '\
                     "detected '%s' as a directory. Usually, it should be a "\
                     'private key file. Please check.',
                     temp_file) if File.directory?(temp_file)
    end
    priv_key_exist = false
    priv_key_name = key_basename
  end

  pub_key_exist = File.exist?(File.join(key_path, key_basename + '.pub'))
  pub_key_name = key_basename + '.pub'

  # keypair basic structure
  { :keypair_name     => keypair_name,
    :keypair_path     => key_path,      :key_basename       => key_basename,
    :private_key_name => priv_key_name, :private_key_exist? => priv_key_exist,
    :public_key_name  => pub_key_name,  :public_key_exist?  => pub_key_exist
  }
end

#keypair_display(keypair) ⇒ Object

Function to display information about keypair object found.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/process/cloud/process/keypairs.rb', line 131

def keypair_display(keypair)
  PrcLib.info("Found keypair '%s'.", keypair[:name])

  unless keypair.exist?(:keypair_path)
    PrcLib.info('Unable to verify your keypair with your local files.'\
                ' :keypair_path is missing.')
    return
  end
  private_key_file = File.join(keypair[:keypair_path],
                               keypair[:private_key_name])
  public_key_file = File.join(keypair[:keypair_path],
                              keypair[:public_key_name])

  PrcLib.info("Found openssh private key file '%s'.",
              private_key_file) if keypair[:private_key_exist?]
  PrcLib.info("Found openssh public key file '%s'.",
              public_key_file) if keypair[:public_key_exist?]

  unless keypair[:public_key_exist?]
    name = keypair[:name]
    PrcLib.warning("The local public key file '%s' is missing.\n"\
                   "As the keypair name '%s' already exists in your cloud, "\
                   'you will need to get the original SSH keypair files '\
                   "used to create the keypair name '%s'. Otherwise, you "\
                   "won't be able to use it to connect to a box configured"\
                   " with '%s'."\
                   "\nPublic key found in the cloud:\n%s",
                   public_key_file, name, name, name,
                   keypair[:public_key])
    return
  end

  if keypair[:coherent]
    PrcLib.info("keypair '%s' local files are coherent with keypair in "\
                'your cloud service. You will be able to use your local '\
                'keys to connect to any box configured with this keypair '\
                'name, over SSH.', keypair[:name])
  else
    PrcLib.warning("Your local public key file '%s' is incoherent with "\
                   "public key attached to the keypair '%s' in your cloud."\
                   " You won't be able to access your box with this keypair."\
                   "\nPublic key found in the cloud:\n%s",
                   public_key_file, keypair[:name], keypair[:public_key])
  end
end

#keypair_files_detected(keypair, loc_kpair) ⇒ Object

Function to update a keypair object with ssh files found in :keypair_path



182
183
184
185
186
187
188
189
# File 'lib/process/cloud/process/keypairs.rb', line 182

def keypair_files_detected(keypair, loc_kpair)
  keypair[:private_key_exist?] = loc_kpair[:private_key_exist?]
  keypair[:public_key_exist?] = loc_kpair[:public_key_exist?]
  keypair[:private_key_name] = loc_kpair[:private_key_name]
  keypair[:public_key_name] = loc_kpair[:public_key_name]
  keypair[:keypair_path] = loc_kpair[:keypair_path]
  keypair[:coherent] = coherent_keypair?(loc_kpair, keypair)
end

#keypair_import(hParams, loc_kpair) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/process/cloud/process/keypairs.rb', line 191

def keypair_import(hParams, loc_kpair)
  PrcLib.fatal(1, "Unable to import keypair '%s'. "\
                  "Public key file '%s' is not found. "\
                  "Please run 'forj setup %s'",
               hParams['credentials#keypair_name'],
               File.join(loc_kpair[:keypair_path],
                         loc_kpair[:public_key_name]),
               config[:account_name]) unless loc_kpair[:public_key_exist?]
  public_key_file = File.join(loc_kpair[:keypair_path],
                              loc_kpair[:public_key_name])

  begin
    public_key = File.read(public_key_file)
  rescue => e
    PrcLib.fatal(1, "Unable to import keypair '%s'. '%s' is "\
                    "unreadable.\n%s", hParams['credentials#keypair_name'],
                 loc_kpair[:public_key_file],
                 e.message)
  end
  keypair = create_keypair(:keypairs, :public_key => public_key)

  return nil if keypair.nil?

  # Adding information about SSH key files.
  keypair_files_detected(keypair, loc_kpair)

  keypair
end

#query_external_network(_hParams) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/process/cloud/process/router.rb', line 244

def query_external_network(_hParams)
  PrcLib.state('Identifying External gateway')
  begin
    # Searching for router port attached
    #################
    query = { :router_external => true }
    networks = controller_query(:network, query)
    case networks.length
    when 0
      PrcLib.info('No external network')
      Lorj::Data.new
    when 1
      PrcLib.info("Found external network '%s'.", networks[0, :name])
      networks[0]
    else
      PrcLib.warn('Found several external networks. Selecting the '\
                  "first one '%s'", networks[0, :name])
      networks[0]
    end
  rescue => e
    PrcLib.error("%s\n%s", e.message, e.backtrace.join("\n"))
  end
end

#query_flavor(sCloudObj, sQuery, hParams) ⇒ Object

Should return 1 or 0 flavor.



45
46
47
48
49
50
# File 'lib/process/cloud/process/flavor.rb', line 45

def query_flavor(sCloudObj, sQuery, hParams)
  flavor_name = hParams['server#flavor_name']
  #  list = forj_query_flavor(sCloudObj, sQuery, hParams)
  #  query_single(sCloudObj, list, sQuery, flavor_name)
  query_single(sCloudObj, sQuery, flavor_name)
end

#query_router_from_port(router_port, _hParams) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/process/cloud/process/router.rb', line 153

def query_router_from_port(router_port, _hParams)
  query = { :id => router_port[:device_id] }
  info = {
    :notfound => 'No %s for port ID %s found',
    :checkmatch => 'Found 1 %s. Checking exact match for port ID %s.',
    :nomatch => 'No %s for port ID %s match',
    :found => "Found %s '%s' from port ID #{router_port[:device_id]}.",
    :more => 'Found several %s. Searching for port ID %s.'
  }
  routers = query_single(:router, query, router_port[:device_id], info)
  return Lorj::Data.new if routers.length == 0
  register(routers[0])
end

#query_subnet(sCloudObj, hParams) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/process/cloud/process/subnetwork.rb', line 80

def query_subnet(sCloudObj, hParams)
  PrcLib.state('Searching for sub-network attached to '\
               "network '%s'", hParams[:network, :name])
  #######################
  begin
    query = { :network_id => hParams[:network, :id] }
    info = {
      :notfound => "No %s found from '%s' network",
      :checkmatch => "Found 1 %s. checking exact match for network '%s'.",
      :nomatch => "No %s for network '%s' match"
    }
    query_single(sCloudObj, query, hParams[:network, :name], info)
  rescue => e
    PrcLib.error("%s\n%s", e.message, e.backtrace.join("\n"))
  end
end

#search_the_image(sCloudObj, sQuery, hParams) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/process/cloud/process/images.rb', line 31

def search_the_image(sCloudObj, sQuery, hParams)
  image_name = hParams['server#image_name']
  images = forj_query_image(sCloudObj, sQuery, hParams)
  case images.length
  when 0
    PrcLib.info("No image '%s' found", image_name)
    nil
  when 1
    PrcLib.info("Found image '%s'.", image_name)
    images[0, :ssh_user] = ssh_user(images[0, :name])
    images[0]
  else
    PrcLib.info("Found several images '%s'. Selecting the first "\
                "one '%s'", image_name, images[0, :name])
    images[0, :ssh_user] = ssh_user(images[0, :name])
    images[0]
  end
end

#ssh_user(image_name) ⇒ Object



67
68
69
70
71
# File 'lib/process/cloud/process/images.rb', line 67

def ssh_user(image_name)
  return 'fedora' if image_name =~ /fedora/i
  return 'centos' if image_name =~ /centos/i
  'ubuntu'
end