Class: OpenStackCredentials

Inherits:
CredentialsInterface show all
Defined in:
lib/ood_core/job/adapters/coder/openstack_credentials.rb

Instance Method Summary collapse

Constructor Details

#initialize(auth_url, dir) ⇒ OpenStackCredentials

Returns a new instance of OpenStackCredentials.



8
9
10
11
# File 'lib/ood_core/job/adapters/coder/openstack_credentials.rb', line 8

def initialize(auth_url, dir)
  @auth_url = auth_url
  @dir = dir
end

Instance Method Details

#destroy_credentials(os_app_credentials, deletion_status, id) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ood_core/job/adapters/coder/openstack_credentials.rb', line 93

def destroy_credentials(os_app_credentials, deletion_status, id)
  return if os_app_credentials.nil?
  

  connection = create_fog_connection(os_app_credentials)
  credentials_to_destroy = find_os_application_credentials(connection, os_app_credentials)

  if deletion_status != "deleted"
    File.delete(file_path(id))
    puts "Workspace deletion timed out, credentials with id #{os_app_credentials['id']} of user #{os_app_credentials['user_id']} were not destroyed"
    return
  end

  begin
    puts "Destroying application credentials with id #{os_app_credentials['id']} and session #{id}}"
    credentials_to_destroy.destroy
  rescue Excon::Error => e
    puts "Error destroying application credentials with id #{os_app_credentials['id']} #{e}"
    raise JobAdapterError, e.message
  end
  File.delete(file_path(id))
end

#file_path(id) ⇒ Object



20
21
22
# File 'lib/ood_core/job/adapters/coder/openstack_credentials.rb', line 20

def file_path(id)
  return "#{@dir}/#{username}-#{id}-os-credentials.json"
end

#generate_credentials(project_id) ⇒ Object



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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ood_core/job/adapters/coder/openstack_credentials.rb', line 24

def generate_credentials(project_id)
  token_json = JSON.parse(File.read("#{@dir}/#{username}-os-token.json"))
  access_token = token_json["id"]
  user_id = token_json["user_id"]
  connection = Fog::OpenStack::Identity.new({
    openstack_auth_url: @auth_url,
    openstack_management_url: @auth_url,
    openstack_auth_token: access_token,
  })

  auth = {
    "auth": {
        "identity": {
            "methods": [
                "token"
            ],
            "token": {
                "id": access_token
            }
        },
        "scope": {
            "project": {
                "id": project_id
            }
        }
    }
  }

  scoped_token = connection.tokens.authenticate(auth)


  connection = Fog::OpenStack::Identity.new({
    openstack_auth_url: @auth_url,
    openstack_management_url: @auth_url,
    openstack_auth_token: scoped_token,
  })


  app_credentials = {
      "name": "OOD_generated_#{rand(16**8).to_s(16)}" ,
      "description": "Application credential generated via OOD for Coder.",
      "roles": [
      ],
      "unrestricted": true,
      "user_id": user_id
  }
  res = connection.application_credentials.create app_credentials

  credential_data = {
    id: res.id,
    name: res.name,
    user_id: user_id,
    secret: res.secret
  }

  credential_data

end

#load_credentials(id) ⇒ Object



13
14
15
16
17
18
# File 'lib/ood_core/job/adapters/coder/openstack_credentials.rb', line 13

def load_credentials(id)
  JSON.parse(File.read(file_path(id)))
rescue Errno::ENOENT => e
  puts "Error loading credentials: #{e}"
  nil
end

#save_credentials(id, app_credentials) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/ood_core/job/adapters/coder/openstack_credentials.rb', line 83

def save_credentials(id, app_credentials)
  Tempfile.open(["temp", ".json"], "/tmp") do |temp_file|
    temp_file.write(JSON.generate(app_credentials))
    temp_file.chmod(0600)
    temp_file.close
    FileUtils.mv(temp_file.path, file_path(id))
  end
end