Class: DOCL::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/docl/cli.rb

Instance Method Summary collapse

Instance Method Details

#authorizeObject



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/docl/cli.rb', line 3

def authorize
    puts "You'll need to enter your DigitalOcean Private Access Token."
    puts "To be able to create / modify droplets, it needs to be read / write."
    puts "You can create a token on the DO website vite the Apps & API menu."

    print "Enter your DO Token: "
    token = $stdin.gets.chomp

    f = File.open(config_path, 'w')
    f.write(token)
    f.close
    File.chmod(0600, config_path)
end

#create(name, image, size, region) ⇒ Object



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
# File 'lib/docl/cli.rb', line 81

def create(name, image, size, region)
    call_options = {
        name: name,
        image: image,
        region: region,
        size: size,
        private_networking: options.private_networking,
        enable_backups: options.enable_backups,
        ipv6: options.ipv6,
    }
    if options[:key]
        call_options[:ssh_keys] = [options[:key]]
    end
    if options.user_data
        call_options[:user_data] = File.read(options.user_data)
    end

    response = barge.droplet.create(call_options)
    display_failure(response) and return if failure?(response)

    if options.wait
        print "Waiting for droplet to become available"
        action_link = response.links.actions.first
        begin
            print '.'
            sleep 1
            action = barge.action.show(action_link.id).action
        end until action.status != 'in-progress'
        puts "Completed"

        puts "You can connect to your Droplet via"
        droplet = barge.droplet.show(response.droplet.id).droplet
        ip_addresses(droplet).each(&method(:puts))
    end
end

#destroy(droplet_id) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/docl/cli.rb', line 133

def destroy(droplet_id)
    if !yes?('This is irreversible, are you sure? (y/n)')
        return
    end
    response = barge.droplet.destroy(droplet_id)
    display_failure(response) and return if failure?(response)
    puts 'Successfully destroyed droplet'
end

#dropletsObject



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/docl/cli.rb', line 118

def droplets
    droplet_response = barge.droplet.all
    display_failure(droplet_response) and return if failure?(droplet_response)
    max_name_width = droplet_response.droplets.map { |droplet| droplet.name.length }.max
    format = "%-10s %-10s %-#{max_name_width + 2}s %-10s %-8s %-8s %-6s %-40s"
    puts format % ["Image", "ID", "Name", "Status", "Region", "Memory", "Disk", "IP Address"]
    droplet_response.droplets.each do |droplet|
        ips = ip_addresses(droplet).join(", ")
        puts format % [droplet.image.distribution, droplet.id, droplet.name,
                       droplet.status, droplet.region.slug,
                       droplet.size_slug.upcase, "#{droplet.disk}GB", ips]
    end
end

#imagesObject



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/docl/cli.rb', line 19

def images()
    image_response = barge.image.all
    display_failure(image_response) and return if failure?(image_response)
    images = image_response.images.select { |image| image.public == options.public }
    images = images.sort { |a, b| a.name <=> b.name }
    images.each do |image|
        if !image.slug.nil?
            puts "#{image.name} (#{image.slug}, id: #{image.id})"
        else
            puts "#{image.name} (id: #{image.id})"
        end
    end
end

#keysObject



34
35
36
37
38
39
40
# File 'lib/docl/cli.rb', line 34

def keys()
    key_response = barge.key.all
    display_failure(key_response) and return if failure?(key_response)
    key_response.ssh_keys.each do |key|
        puts "#{key.name} (id: #{key.id})"
    end
end

#regionsObject



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
# File 'lib/docl/cli.rb', line 47

def regions()
    region_response = barge.region.all
    display_failure(region_response) and return if failure?(region_response)
    regions = region_response.regions.select do |region|
        if options.ipv6 && !region.features.include?('ipv6')
            next false
        end

        if options. && !region.features.include?('metadata')
            next false
        end

        if options.private_networking && !region.features.include?('private_networking')
            next false
        end

        if options.backups && !region.features.include?('backups')
            next false
        end

        next true
    end
    regions.sort { |a, b| a.name <=> b.name }.each do |region|
        puts "#{region.name} (#{region.slug})"
    end
end