Class: Tenma::Ichiba::Instance
- Inherits:
-
Object
- Object
- Tenma::Ichiba::Instance
- Defined in:
- lib/tenma/ichiba/instance.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#disk_size ⇒ Object
readonly
Returns the value of attribute disk_size.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#project ⇒ Object
readonly
Returns the value of attribute project.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#zone ⇒ Object
readonly
Returns the value of attribute zone.
Instance Method Summary collapse
- #create ⇒ Object
- #delete ⇒ Object
-
#initialize(context) ⇒ Instance
constructor
A new instance of Instance.
- #provision ⇒ Object
- #restart ⇒ Object
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..raw.instance_name @type = @context..raw.instance_machine_type @zone = @context..raw.instance_zone @project = @context..raw.instance_project @disk_size = @context..raw.instance_disk_size end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
7 8 9 |
# File 'lib/tenma/ichiba/instance.rb', line 7 def context @context end |
#disk_size ⇒ Object (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 |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/tenma/ichiba/instance.rb', line 7 def name @name end |
#project ⇒ Object (readonly)
Returns the value of attribute project.
7 8 9 |
# File 'lib/tenma/ichiba/instance.rb', line 7 def project @project end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
7 8 9 |
# File 'lib/tenma/ichiba/instance.rb', line 7 def type @type end |
#zone ⇒ Object (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
#create ⇒ Object
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 |
#delete ⇒ Object
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 |
#provision ⇒ Object
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..raw.ssh_key_file if @context..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.('../itamae/roles/remote.rb', __FILE__) node_yaml = context..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 |
#restart ⇒ Object
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 |