Class: Kitsune::Kit::Provisioner

Inherits:
Object
  • Object
show all
Defined in:
lib/kitsune/kit/provisioner.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Provisioner

Returns a new instance of Provisioner.



6
7
8
9
10
11
12
13
14
15
# File 'lib/kitsune/kit/provisioner.rb', line 6

def initialize(opts)
  @droplet_name = opts[:droplet_name]
  @region       = opts[:region]
  @size         = opts[:size]
  @image        = opts[:image]
  @tag          = opts[:tag]
  @ssh_key_id   = opts[:ssh_key_id] { abort "❌ You must export SSH_KEY_ID or use --ssh_key_id" }

  @client       = DropletKit::Client.new(access_token: ENV.fetch("DO_API_TOKEN"))
end

Instance Method Details

#create_or_showObject

Create command: shows if it exists or creates a new one



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

def create_or_show
  if (d = find_droplet)
    ip = public_ip(d)
    puts "✅ Droplet '#{@droplet_name}' already exists (ID: #{d.id}, IP: #{ip})"
  else
    puts "✍🏻 Creating Droplet '#{@droplet_name}'..."
    spec = DropletKit::Droplet.new(
      name:       @droplet_name,
      region:     @region,
      size:       @size,
      image:      @image,
      ssh_keys:   [@ssh_key_id],
      tags:       [@tag]
    )
    created = @client.droplets.create(spec)
    wait_for_status(created.id)
    ip = wait_for_public_ip(created.id)

    wait_for_ssh(ip)
  
    puts "✅ Droplet created: ID=#{created.id}, IP=#{ip}"
  end
end

#find_dropletObject

Find an existing droplet by name



18
19
20
# File 'lib/kitsune/kit/provisioner.rb', line 18

def find_droplet
  @client.droplets.all(tag_name: @tag).detect { |d| d.name == @droplet_name }
end

#rollbackObject

Rollback command: deletes it if it exists



48
49
50
51
52
53
54
55
56
# File 'lib/kitsune/kit/provisioner.rb', line 48

def rollback
  if (d = find_droplet)
    puts "🔁 Deleting Droplet '#{@droplet_name}' (ID: #{d.id})..."
    @client.droplets.delete(id: d.id)
    puts "✅ Droplet deleted 💥"
  else
    puts "✅ Nothing to delete: '#{@droplet_name}' does not exist"
  end
end