Class: Tenma::Ichiba::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/tenma/ichiba/instance.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Instance

Returns a new instance of Instance.



8
9
10
11
12
13
14
15
# File 'lib/tenma/ichiba/instance.rb', line 8

def initialize(context)
  @context = context
  @name = @context.options.raw.instance_name
  @type = @context.options.raw.instance_machine_type
  @zone = @context.options.raw.instance_zone
  @project = @context.options.raw.instance_project
  @disk_size = @context.options.raw.instance_disk_size
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



7
8
9
# File 'lib/tenma/ichiba/instance.rb', line 7

def context
  @context
end

#disk_sizeObject (readonly)

Returns the value of attribute disk_size.



7
8
9
# File 'lib/tenma/ichiba/instance.rb', line 7

def disk_size
  @disk_size
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/tenma/ichiba/instance.rb', line 7

def name
  @name
end

#projectObject (readonly)

Returns the value of attribute project.



7
8
9
# File 'lib/tenma/ichiba/instance.rb', line 7

def project
  @project
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/tenma/ichiba/instance.rb', line 7

def type
  @type
end

#zoneObject (readonly)

Returns the value of attribute zone.



7
8
9
# File 'lib/tenma/ichiba/instance.rb', line 7

def zone
  @zone
end

Instance Method Details

#createObject



17
18
19
20
# File 'lib/tenma/ichiba/instance.rb', line 17

def create
  puts "Create instance..."
  puts `gcloud compute instances create #{name} --image-family ubuntu-1710 --image-project ubuntu-os-cloud --preemptible --machine-type #{type} --zone #{zone} --project #{project} --boot-disk-size #{disk_size}GB`
end

#deleteObject



48
49
50
51
# File 'lib/tenma/ichiba/instance.rb', line 48

def delete
  puts "Delete instance..."
  puts `gcloud compute instances delete #{name} --delete-disks all --quiet --zone #{zone} --project #{project}`
end

#provisionObject



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
# File 'lib/tenma/ichiba/instance.rb', line 22

def provision
  file = context.options.raw.ssh_key_file

  if @context.options.create_instance?
    # Immediately after launching the instance, it takes time to start up, so it can not lead to SSH.
    puts "sleep 60 sec"
    sleep 60 # sec
  end

  # In order to execute Itamae, it is necessary to set up ssh config.
  puts `gcloud compute config-ssh --remove --ssh-key-file #{file} --project #{project}`
  puts `gcloud compute config-ssh --ssh-key-file #{file} --project #{project}`

  puts "Provision instance..."
  role_file = File.expand_path('../itamae/roles/remote.rb', __FILE__)
  node_yaml = context.options.raw.node_yaml
  Itamae::Runner.run([role_file], :ssh, {
    host: "#{name}.#{zone}.#{project}",
    node_yaml: node_yaml,
    color: false,
    sudo: true,
    shell: "/bin/sh",
    log_level: "info",
  })
end

#restartObject



53
54
55
56
57
58
# File 'lib/tenma/ichiba/instance.rb', line 53

def restart
  puts "Reset instance..."
  puts `gcloud compute instances reset #{name} --zone #{zone} --project #{project}`
  puts "Start instance..."
  puts `gcloud compute instances start #{name} --zone #{zone} --project #{project}`
end