-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdo-ipchange.rb
More file actions
134 lines (123 loc) · 3.2 KB
/
do-ipchange.rb
File metadata and controls
134 lines (123 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#/!/usr/bin/ruby -w
# The setup
require 'time'
require 'droplet_kit'
require 'json'
if ARGV.length != 2
puts "Usage: do-ipchange <api-token> <droplet name>"
exit
end
Timestamp = Time.now.utc.iso8601
Token=ARGV[0]
Droplet_name=ARGV[1]
Client = DropletKit::Client.new(access_token: Token)
# Get the ID, size and location of the droplet we are doing this on
def get_droplet_id()
droplets = Client.droplets.all
droplets.each do |droplet|
if droplet.name == Droplet_name
puts " - Droplet id: #{droplet.id}"
puts " - Droplet size_slug: #{droplet.size_slug}"
puts " - Droplet region: #{droplet.region.slug}"
@droplet_id = droplet.id
@droplet_size = droplet.size_slug
@droplet_region = droplet.region.slug
end
end
rescue NoMethodError
puts JSON.parse(droplets)['message']
end
# Shut down the droplet
def shutdown(id)
res = Client.droplet_actions.shutdown(droplet_id: id)
until res.status == "completed"
res = Client.actions.find(id: res.id)
sleep(2)
end
puts " * Action status: #{res.status}"
rescue NoMethodError
puts JSON.parse(res)['message']
end
# Create snapshot
def take_snapshot(id, name)
res = Client.droplet_actions.snapshot(droplet_id: id, name: name)
until res.status == "completed"
res = Client.actions.find(id: res.id)
sleep(2)
end
puts " * Action status: #{res.status}"
rescue NameError
puts JSON.parse(res)['message']
end
# Delete incumbent droplet
def delete_droplet(id)
res = Client.droplets.delete(id: id)
until res == True
sleep(2)
end
puts res
end
# Create new droplet from snapshot
def deploy_droplet()
images = Client.images.all(public:false)
images.each do |image|
if image.name == Timestamp
@image_id = image.id
puts @image_id
end
end
droplet = DropletKit::Droplet.new(name: Droplet_name, region: @droplet_region, size: @droplet_size, image: @image_id)
res = Client.droplets.create(droplet)
puts " * Action status: #{res.status}"
rescue NameError
puts JSON.parse(res)['message']
end
# Delete snapshot
def delete_snapshot()
res = Client.images.delete(id: @image_id)
until res == true
sleep(2)
end
puts " * Action status: #{res.status}"
rescue NameError
puts res
end
# Get IP of new droplet
def get_IP()
droplets = Client.droplets.all
droplets.each do |droplet|
if droplet.name == Droplet_name
puts " - Droplet ID: #{droplet.id}"
droplet.networks.each do |network|
network.each do |net|
if net["type"] == 'public'
puts " - Droplet IP: #{net["ip_address"]}"
end
end
end
end
end
rescue NameError
puts "Something went wrong in get_IP()"
end
puts "Getting droplet id..."
get_droplet_id()
puts "Powering off droplet..."
shutdown(@droplet_id)
sleep(2)
puts "Taking snapshot..."
take_snapshot(@droplet_id, Timestamp)
sleep(2)
puts "Deleting droplet..."
delete_droplet(@droplet_id)
sleep(2)
puts "Creating new droplet from image..."
deploy_droplet()
sleep(2)
puts "Deleting snapshot..."
delete_snapshot()
puts "Waiting 30s for new droplet to deploy..."
sleep(30)
puts "Getting details of new droplet..."
get_IP()
puts " Complete"