Class: WcoHosting::Serverhost

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Paranoia, Mongoid::Timestamps
Defined in:
app/models/wco_hosting/serverhost.rb

Constant Summary collapse

WORKDIR =
"/opt/projects/docker"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.do_list_keysObject



35
36
37
38
39
40
# File 'app/models/wco_hosting/serverhost.rb', line 35

def self.do_list_keys
  out = HTTParty.get( "https://api.digitalocean.com/v2/account/keys",
    headers: { 'Authorization' => "Bearer #{DO_READER_TOKEN}" },
    :debug_output => $stdout,
  );
end

.listObject



42
43
44
45
# File 'app/models/wco_hosting/serverhost.rb', line 42

def self.list
  [[nil,nil]] + all.map { |s| [s.name, s.id] }
  # all.map { |s| [s.name, s.id] }
end

Instance Method Details

#add_docker_service(app) ⇒ Object

@TODO: this can be very different, depending on kindset.



50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/wco_hosting/serverhost.rb', line 50

def add_docker_service app
  @obj = app
  cmd =<<~AOL
    cd #{ANSIBLE_ROOT}
    . zenv/bin/activate
    ansible-playbook -i inventory/do.yml --limit #{self.name} playbooks/#{app.playbook_name}.yml --extra-vars '{
      "appliance_slug": "#{app.slug}",
      "codebase_zip": "#{app.tmpl.volume_zip_url}",
      "app_port": "#{app.port}"}'
  AOL
  do_exec cmd
end

#add_nginx_site(app) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'app/models/wco_hosting/serverhost.rb', line 63

def add_nginx_site app
  @obj = app
  cmd =<<~AOL
    cd #{ANSIBLE_ROOT}
    . zenv/bin/activate
    ansible-playbook -i inventory/do.yml --limit #{self.name} playbooks/install-nginx-proxy-vsite.yml --extra-vars '{"appliance_slug": "#{app.slug}", "port": "#{app.port}", "origin": "#{app.subdomain}.#{app.domain.name}" }'
  AOL
  do_exec cmd
end

#create_subdomain(app) ⇒ Object

def create_appliance app

  # puts! app, 'Serverhost#create_appliance'
  create_subdomain(   app )
  create_volume(      app )
  add_docker_service( app )
  add_nginx_site(     app )
  # load_database( app )
end


84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/models/wco_hosting/serverhost.rb', line 84

def create_subdomain app
  @obj = app
  Wco::Log.puts! @obj, 'Creating subdomain...', obj: @obj
  client = DropletKit::Client.new(access_token: ::DO_DOMAIN_TOKEN)
  record = DropletKit::DomainRecord.new(
    type: 'A',
    name: app.subdomain,
    data: app.serverhost.public_ip,
  )
  client.domain_records.create(record, for_domain: app.domain )
  Wco::Log.puts! record, 'Created subdomain.', obj: @obj
  # WcoHosting::Subdomain.create!( domain: app.domain, name: app.subdomain )
end

#do_create_server!Object



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
# File 'app/models/wco_hosting/serverhost.rb', line 98

def do_create_server!
  out = HTTParty.post( "https://api.digitalocean.com/v2/droplets", body: {
      monitoring: true,
      name: name,
      size: "s-1vcpu-2gb",
      region: "sfo2",
      image: "ubuntu-22-04-x64",
      ssh_keys: [ 40460634 ], # 2np, "71:9a:7e:9a:54:55:af:d0:16:47:5c:57:a8:c3:f0:2b"
      vpc_uuid: "b9f748b6-4649-4506-a457-b70d2b57b313",
    }.to_json,
    headers: { 'Content-Type'  => 'application/json',
               'Authorization' => "Bearer #{DO_DROPLET_TOKEN}" },
    :debug_output => $stdout,
  );
  puts! out, '#create_do_server()'
  out = out.parsed_response
  self.do_id = out['droplet']['id']
  self.save

  do_project_id = '4dad5c55-1a11-4c2b-9e14-1cd69513d940' # WcoHosting DO project id
  out = HTTParty.post( "https://api.digitalocean.com/v2/projects/#{do_project_id}/resources", body: {
      resources: [ "do:droplet:#{out['droplet']['id']}" ],
    }.to_json,
    headers: { 'Content-Type'  => 'application/json',
               'Authorization' => "Bearer #{DO_DROPLET_TOKEN}" },
    :debug_output => $stdout,
  );
  puts! out, 'assign droplet to a project'
end

#do_exec(cmd) ⇒ Object



128
129
130
131
132
133
# File 'app/models/wco_hosting/serverhost.rb', line 128

def do_exec cmd
  Wco::Log.puts! cmd, '#do_exec', obj: @obj
  IO.popen(cmd).each do |line|
    Wco::Log.puts line, obj: @obj
  end
end

#ssh_hostObject

net-ssh, sshkit



26
# File 'app/models/wco_hosting/serverhost.rb', line 26

field :ssh_host

#to_sObject



135
136
137
# File 'app/models/wco_hosting/serverhost.rb', line 135

def to_s
  "<Serverhost name=#{name} next_port=#{next_port} ssh_host=#{ssh_host} />"
end