@@ -13,6 +13,7 @@ def self.list(verbose, url, os_filter = nil)
13
13
os_list = [ ]
14
14
15
15
response = conn . get 'status/platforms/vmpooler'
16
+
16
17
response_body = JSON . parse ( response . body )
17
18
os_list << "*** VMPOOLER Pools ***"
18
19
os_list = os_list + JSON . parse ( response_body [ "vmpooler_platforms" ] )
@@ -31,89 +32,98 @@ def self.list(verbose, url, os_filter = nil)
31
32
32
33
os_list . delete 'ok'
33
34
34
- puts os_list
35
-
36
35
os_filter ? os_list . select { |i | i [ /#{ os_filter } / ] } : os_list
37
36
end
38
37
39
- # List active VMs from ABS
40
- def self . list_active ( verbose , url , token )
41
- status = Auth . token_status ( verbose , url , token )
42
- status [ 'reserved_hosts' ] || [ ]
43
- end
38
+ # Retrieve an OS from ABS.
39
+ def self . retrieve ( verbose , os_types , token , url , user )
40
+ #
41
+ # Contents of post must be:j
42
+ #
43
+ # {
44
+ # "resources": {
45
+ # "centos-7-i386": 1,
46
+ # "ubuntu-1404-x86_64": 2
47
+ # },
48
+ # "job": {
49
+ # "id": "12345",
50
+ # "tags": {
51
+ # "user": "jenkins",
52
+ # "jenkins_build_url": "https://jenkins/job/platform_puppet_intn-van-sys_master"
53
+ # }
54
+ # }
55
+ # }
44
56
45
- def self . retrieve ( verbose , os_type , token , url )
46
57
conn = Http . get_conn ( verbose , url )
47
58
conn . headers [ 'X-AUTH-TOKEN' ] = token if token
48
59
49
- os_string = os_type . map { |os , num | Array ( os ) * num } . flatten . join ( '+' )
50
- raise MissingParamError , 'No operating systems provided to obtain.' if os_string . empty?
51
-
52
- response = conn . post "host/#{ os_string } "
53
-
54
- res_body = JSON . parse ( response . body )
60
+ saved_job_id = Time . now . to_i
55
61
56
- if res_body [ 'ok' ]
57
- res_body
58
- elsif response . status == 401
59
- raise AuthError , "HTTP #{ response . status } : The token provided could not authenticate to the pooler.\n #{ res_body } "
60
- else
61
- raise "HTTP #{ response . status } : Failed to obtain VMs from the pooler at #{ url } /host/#{ os_string } . #{ res_body } "
62
- end
63
- end
62
+ reqObj = {
63
+ resources : os_types ,
64
+ job : {
65
+ id : saved_job_id ,
66
+ tags : {
67
+ user : user ,
68
+ url_string : "floaty://#{ user } /#{ saved_job_id } "
69
+ }
70
+ }
71
+ }
64
72
65
- def self . modify ( verbose , url , hostname , token , modify_hash )
66
- raise TokenError , 'Token provided was nil; Request cannot be made to modify VM' if token . nil?
73
+ # os_string = os_type.map { |os, num| Array(os) * num }.flatten.join('+')
74
+ # raise MissingParamError, 'No operating systems provided to obtain.' if os_string.empty?
75
+ puts "Requesting VMs with job_id: #{ saved_job_id } . Will retry for up to an hour."
76
+ response = conn . post "api/v2/request" , reqObj . to_json
67
77
68
- modify_hash . each do |key , _value |
69
- raise ModifyError , "Configured service type does not support modification of #{ key } " unless %i[ reason reserved_for_reason ] . include? key
70
- end
78
+ i = 0
79
+ retries = 360
71
80
72
- if modify_hash [ :reason ]
73
- # "reason" is easier to type than "reserved_for_reason", but nspooler needs the latter
74
- modify_hash [ :reserved_for_reason ] = modify_hash . delete :reason
81
+ if response . status == 401
82
+ raise AuthError , "HTTP #{ response . status } : The token provided could not authenticate to the pooler.\n #{ res_body } "
75
83
end
76
84
77
- conn = Http . get_conn ( verbose , url )
78
- conn . headers [ 'X-AUTH-TOKEN' ] = token
85
+ ( 1 ..retries ) . each do |i |
86
+ queue_place , res_body = check_queue ( conn , saved_job_id , reqObj )
87
+ if res_body
88
+ return translated ( res_body )
89
+ end
79
90
80
- response = conn . put do |req |
81
- req . url "host/#{ hostname } "
82
- req . body = modify_hash . to_json
91
+ puts "Waiting 10 seconds to check if ABS request has been filled. Queue Position: #{ queue_place } ... (x#{ i } )"
92
+ sleep ( 10 )
83
93
end
84
-
85
- response . body . empty? ? { } : JSON . parse ( response . body )
94
+ return nil
86
95
end
87
96
88
- def self . disk ( _verbose , _url , _hostname , _token , _disk )
89
- raise ModifyError , 'Configured service type does not support modification of disk space'
90
- end
91
-
92
- def self . snapshot ( _verbose , _url , _hostname , _token )
93
- raise ModifyError , 'Configured service type does not support snapshots'
94
- end
97
+ #
98
+ # We should fix the ABS API to be more like the vmpooler or nspooler api, but for now
99
+ #
100
+ def self . translated res_body
101
+ vmpooler_formatted_body = Hash . new
102
+
103
+ res_body . each do |host |
104
+ if vmpooler_formatted_body [ host [ "type" ] ] && vmpooler_formatted_body [ host [ "type" ] ] [ "hostname" ] . class == Array
105
+ vmpooler_formatted_body [ host [ "type" ] ] [ "hostname" ] << host [ "hostname" ]
106
+ else
107
+ vmpooler_formatted_body [ host [ "type" ] ] = { "hostname" => [ host [ "hostname" ] ] }
108
+ end
109
+ end
110
+ vmpooler_formatted_body [ "ok" ] = true
95
111
96
- def self . revert ( _verbose , _url , _hostname , _token , _snapshot_sha )
97
- raise ModifyError , 'Configured service type does not support snapshots'
112
+ return vmpooler_formatted_body
98
113
end
99
114
100
- def self . delete ( verbose , url , hosts , token )
101
- raise TokenError , 'Token provided was nil; Request cannot be made to delete VM' if token . nil?
115
+ def self . check_queue conn , job_id , reqObj
116
+ queue_info_response = conn . get "/status/queue/info/#{ job_id } "
117
+ queue_info = JSON . parse ( queue_info_response . body )
102
118
103
- conn = Http . get_conn ( verbose , url )
119
+ response = conn . post "api/v2/request" , reqObj . to_json
104
120
105
- conn . headers [ 'X-AUTH-TOKEN' ] = token if token
106
121
107
- response_body = { }
108
-
109
- hosts = hosts . split ( ',' ) unless hosts . is_a? Array
110
- hosts . each do |host |
111
- response = conn . delete "host/#{ host } "
122
+ if response . body . length > 0
112
123
res_body = JSON . parse ( response . body )
113
- response_body [ host ] = res_body
124
+ return queue_info [ "queue_place" ] , res_body
114
125
end
115
-
116
- response_body
126
+ return queue_info [ "queue_place" ] , nil
117
127
end
118
128
119
129
def self . status ( verbose , url )
0 commit comments