Class: CloudProcess

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

Overview

External network process attached to a network

Instance Method Summary collapse

Instance Method Details

#_check_key_file(key_path, key_basename, extensions) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/core_process/cloud/process/keypairs.rb', line 174

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

#add_ssh_user(images) ⇒ Object



61
62
63
64
65
# File 'lib/core_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



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

def assign_address(sCloudObj, hParams)
  name = hParams[:server, :name]
  begin
    PrcLib.state('Getting public IP for server %s', name)
    ip_address = controller_create(sCloudObj)
    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)


226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/core_process/cloud/process/keypairs.rb', line 226

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

  keypair_name = loc_kpair[:keypair_name]
  is_coherent = false

  pub_keypair = keypair[:public_key]

  # Check the public key with the one found here, locally.
  if !pub_keypair.nil? && pub_keypair != ''
    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
        PrcLib.info("keypair '%s' local files are coherent with keypair in "\
                    'your cloud service. You will be able to connect to '\
                    'your box over SSH.', keypair_name)
        is_coherent = true
      else
        PrcLib.warning("Your local keypair file '%s' are incoherent with "\
                       "public key '%s' found in your cloud. You won't be "\
                       "able to access your box with this keypair.\nPublic "\
                       "key found in the cloud:\n%s",
                       loc_kpair[:public_key_file], keypair_name,
                       keypair[:public_key])
      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/core_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[: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



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/core_process/cloud/process/keypairs.rb', line 122

def create_keypair(sCloudObj, hParams)
  key_name = hParams[:keypair_name]
  PrcLib.state("Importing keypair '%s'", key_name)
  ssl_error_obj = SSLErrorMgt.new
  begin
    keypair = controller_create(sCloudObj)
    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/core_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



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

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



165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/core_process/cloud/process/router.rb', line 165

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/core_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



110
111
112
113
114
115
116
117
118
119
# File 'lib/core_process/cloud/process/security_groups.rb', line 110

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



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

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/core_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



138
139
140
141
142
143
144
145
146
147
# File 'lib/core_process/cloud/process/router.rb', line 138

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



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/core_process/cloud/process/router.rb', line 179

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/core_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/core_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/core_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/core_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



38
39
40
41
42
43
44
45
46
# File 'lib/core_process/cloud/process/server.rb', line 38

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



62
63
64
65
66
67
68
69
# File 'lib/core_process/cloud/process/security_groups.rb', line 62

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/core_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, sName, _hParams) ⇒ Object



187
188
189
190
191
192
193
194
# File 'lib/core_process/cloud/process/keypairs.rb', line 187

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

#forj_get_network(sCloudObj, sID, hParams) ⇒ Object



53
54
55
56
57
# File 'lib/core_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/core_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/core_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/core_process/cloud/process/flavor.rb', line 26

def forj_get_or_create_flavor(sCloudObj, hParams)
  flavor_name = hParams[: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/core_process/cloud/process/images.rb', line 23

def forj_get_or_create_image(sCloudObj, hParams)
  image_name = hParams[: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

  • 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/core_process/cloud/process/keypairs.rb', line 41

def forj_get_or_create_keypair(sCloudObj, hParams)
  keypair_name = hParams[:keypair_name]
  # setup has configured and copied the appropriate key to forj keypairs.

  loc_kpair = keypair_detect(keypair_name,
                             File.expand_path(hParams[:keypair_path]))

  private_key_file = File.join(loc_kpair[:keypair_path],
                               loc_kpair[:private_key_name])
  public_key_file = File.join(loc_kpair[:keypair_path],
                              loc_kpair[:public_key_name])

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

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

  keypairs = forj_query_keypair(sCloudObj,
                                { :name => keypair_name }, hParams)

  if keypairs.length == 0
    keypair = keypair_import(hParams, loc_kpair)
  else
    keypair = keypairs[0]
    keypair[:coherent] = coherent_keypair?(loc_kpair, keypair)
    # Adding information about key files.
  end
  if keypair[:coherent]
    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]
  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/core_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
37
38
39
40
41
42
43
44
45
46
# File 'lib/core_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

  router_port = get_router_interface_attached(:port, hParams)

  if 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

#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/core_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
# File 'lib/core_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)
  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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/core_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)
  ports = config.get(: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
  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/core_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



57
58
59
60
61
62
63
64
# File 'lib/core_process/cloud/process/public_ip.rb', line 57

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



57
58
59
60
61
62
63
64
# File 'lib/core_process/cloud/process/server.rb', line 57

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



118
119
120
121
122
123
124
125
# File 'lib/core_process/cloud/process/server.rb', line 118

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/core_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/core_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/core_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_keypair(sCloudObj, sQuery, hParams) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/core_process/cloud/process/keypairs.rb', line 78

def forj_query_keypair(sCloudObj, sQuery, hParams)
  key_name = hParams[:keypair_name]
  ssl_error_obj = SSLErrorMgt.new
  begin
    #  list = controller_query(sCloudObj, sQuery)
    #  query_single(sCloudObj, list, sQuery, key_name)
    query_single(sCloudObj, sQuery, key_name)
  rescue => e
    retry unless ssl_error_obj.error_detected(e.message, e.backtrace, e)
  end
end

#forj_query_public_address(sCloudObj, sQuery, hParams) ⇒ Object



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

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/core_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



48
49
50
51
52
53
54
55
# File 'lib/core_process/cloud/process/server.rb', line 48

def forj_query_server(sCloudObj, sQuery, _hParams)
  ssl_error_obj = SSLErrorMgt.new
  begin
    controller_query(sCloudObj, sQuery)
  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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/core_process/cloud/process/security_groups.rb', line 72

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

#get_gateway(net_conn_obj, name) ⇒ Object

Gateway management



216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/core_process/cloud/process/router.rb', line 216

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



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

def get_keypairs_path(hParams, hKeys)
  keypair_name = hParams[: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



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/core_process/cloud/process/router.rb', line 99

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



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

def get_router_interface_attached(sCloudObj, hParams)
  name = hParams[:network, :name]
  PrcLib.state("Searching for router port attached to the network '%s'", name)
  begin
    # Searching for router port attached
    #################
    query = { :network_id => hParams[
                             :network, :id],
              :device_owner => 'network:router_interface' }
    info = {
      :notfound => "No router %s for network '%s' found",
      :checkmatch => 'Found 1 router %s. '\
                           "Checking exact match for network '%s'.",
      :nomatch => "No router %s for network '%s' match",
      :found => "Found router %s ID %s attached to network '#{name}'.",
      :more => "Found several router %s. Searching for network '%s'.",
      :items => [:id]
    }
    query_single(sCloudObj, query, name, info)
  rescue => e
    PrcLib.error("%s\n%s", e.message, e.backtrace.join("\n"))
  end
end

#keypair_detect(keypair_name, key_fullpath) ⇒ Object

Build keypair data information structure with files found in local filesystem. Take care of priv with or without .pem and pubkey with pub.



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
# File 'lib/core_process/cloud/process/keypairs.rb', line 139

def keypair_detect(keypair_name, key_fullpath)
  key_basename = File.basename(key_fullpath)
  key_path = File.expand_path(File.dirname(key_fullpath))

  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_import(hParams, loc_kpair) ⇒ Object



93
94
95
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
# File 'lib/core_process/cloud/process/keypairs.rb', line 93

def keypair_import(hParams, loc_kpair)
  PrcLib.fatal(1, "Unable to import keypair '%s'. "\
                  'Public key file is not found. '\
                  "Please run 'forj setup %s'",
               hParams[:keypair_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
    config[:public_key] = File.read(public_key_file)
  rescue => e
    PrcLib.fatal(1, "Unable to import keypair '%s'. '%s' is "\
                    "unreadable.\n%s", hParams[:keypair_name],
                 loc_kpair[:public_key_file],
                 e.message)
  end
  keypair = create_keypair(:keypairs, hParams)

  return nil if keypair.nil?

  if !loc_kpair[:private_key_exist?]
    keypair[:coherent] = false
  else
    keypair[:coherent] = true
  end
  keypair
end

#query_external_network(_hParams) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/core_process/cloud/process/router.rb', line 231

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/core_process/cloud/process/flavor.rb', line 45

def query_flavor(sCloudObj, sQuery, hParams)
  flavor_name = hParams[: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



149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/core_process/cloud/process/router.rb', line 149

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/core_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/core_process/cloud/process/images.rb', line 31

def search_the_image(sCloudObj, sQuery, hParams)
  image_name = hParams[: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/core_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