Class: WcoHosting::Serverhost

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, 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

.listObject



174
175
176
177
# File 'app/models/wco_hosting/serverhost.rb', line 174

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



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

def add_docker_service app
  puts! app, '#add_docker_service'

  ac   = ActionController::Base.new
  ac.instance_variable_set( :@app, app )
  ac.instance_variable_set( :@workdir, WORKDIR )
  rendered_str = ac.render_to_string("wco_hosting/docker-compose/dc-#{app.tmpl.kind}")
  puts '+++ add_docker_service rendered_str:'
  print rendered_str

  file = Tempfile.new('prefix')
  file.write rendered_str
  file.close
  puts! file.path, 'file.path'

  cmd = "scp #{file.path} #{ssh_host}:#{WORKDIR}/dc-#{app.service_name}.yml "
  puts! cmd, 'cmd'
  `#{cmd}`

  cmd = "ssh #{ssh_host} 'cd #{WORKDIR} ; \
    docker compose -f dc-#{app.service_name}.yml up -d #{app.service_name} ; \
    echo ok #add_docker_service ' "
  puts! cmd, 'cmd'
  `#{cmd}`

  puts "ok '#add_docker_service'"
end

#add_nginx_site(app) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/models/wco_hosting/serverhost.rb', line 72

def add_nginx_site app
  ac   = ActionController::Base.new
  ac.instance_variable_set( :@app, app )
  rendered_str = ac.render_to_string("wco_hosting/scripts/nginx_site.conf")
  puts '+++ add_nginx_site rendered_str:'
  print rendered_str

  file = Tempfile.new('prefix')
  file.write rendered_str
  file.close

  cmd = "scp #{file.path} #{ssh_host}:/etc/nginx/sites-available/#{app.service_name}.conf "
  puts! cmd, 'cmd'
  `#{cmd}`

  cmd = "ssh #{ssh_host} 'ln -s /etc/nginx/sites-available/#{app.service_name}.conf /etc/nginx/sites-enabled/#{app.service_name}.conf ' "
  puts! cmd, 'cmd'
  `#{cmd}`

  cmd = "ssh #{ssh_host} 'nginx -s reload ' "
  puts! cmd, 'cmd'
  `#{cmd}`
end

#aws_create_subdomain(app) ⇒ Object

obsolete vp 2023-12-23



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/wco_hosting/serverhost.rb', line 54

def aws_create_subdomain app
  ac   = ActionController::Base.new
  ac.instance_variable_set( :@app, app )
  rendered_str = ac.render_to_string("wco_hosting/scripts/create_subdomain.json")
  # puts '+++ create_subdomain rendered_str:'; print rendered_str

  file = Tempfile.new('prefix')
  file.write rendered_str
  file.close

  cmd = "aws route53 change-resource-record-sets \
    --hosted-zone-id #{ app.route53_zone } \
    --change-batch file://#{ file.path } \
    --profile route53 "
  do_exec( cmd )
end

#create_appliance(app) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/wco_hosting/serverhost.rb', line 31

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 )

  update({ next_port: app.serverhost.next_port + 1 })
end

#create_subdomain(app) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'app/models/wco_hosting/serverhost.rb', line 43

def create_subdomain app
  client = DropletKit::Client.new(access_token: DO_TOKEN_1)
  record = DropletKit::DomainRecord.new(
    type: 'A',
    name: app.subdomain,
    data: app.serverhost.public_ip,
  )
  client.domain_records.create(record, for_domain: app.domain )
end

#create_volume(app) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'app/models/wco_hosting/serverhost.rb', line 149

def create_volume app
  # puts! app.service_name, 'Serverhost#create_volume'

  ac   = ActionController::Base.new
  ac.instance_variable_set( :@app, app )
  ac.instance_variable_set( :@workdir, WORKDIR )
  rendered_str = ac.render_to_string("wco_hosting/scripts/create_volume")
  puts '+++ create_volume rendered_str:'
  print rendered_str

  file = Tempfile.new('prefix')
  file.write rendered_str
  file.close
  # puts! file.path, 'file.path'

  cmd = "scp #{file.path} #{ssh_host}:#{WORKDIR}/scripts/create_volume"
  do_exec( cmd )

  cmd = "ssh #{ssh_host} 'chmod a+x #{WORKDIR}/scripts/create_volume ; #{WORKDIR}/scripts/create_volume ' "
  do_exec( cmd )

  puts 'ok #create_volume'
  return done_exec
end

#create_wordpress_volume(app) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/models/wco_hosting/serverhost.rb', line 124

def create_wordpress_volume app
  ac   = ActionController::Base.new
  ac.instance_variable_set( :@app, app )
  ac.instance_variable_set( :@workdir, WORKDIR )
  rendered_str = ac.render_to_string("wco_hosting/scripts/create_volume")
  # puts '+++ create_volume rendered_str:'
  # print rendered_str

  file = Tempfile.new('prefix')
  file.write rendered_str
  file.close
  # puts! file.path, 'file.path'

  cmd = "scp #{file.path} #{ssh_host}:#{WORKDIR}/scripts/create_volume"
  puts! cmd, 'cmd'
  `#{cmd}`

  cmd = "ssh #{ssh_host} 'chmod a+x #{WORKDIR}/scripts/create_volume ; \
    #{WORKDIR}/wco_hosting/scripts/create_volume ' "
  puts! cmd, 'cmd'
  `#{cmd}`

  puts 'ok #create_volume'
end

#do_exec(cmd) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'app/models/wco_hosting/serverhost.rb', line 179

def do_exec cmd
  # @messages ||= []
  # @errors   ||= []
  # @statuses ||= []

  puts! cmd, '#do_exec'
  stdout, stderr, status = Open3.capture3(cmd)
  status = status.to_s.split.last.to_i
  puts "+++ +++ stdout"
  puts stdout
  # @messages.push( stdout )
  puts "+++ +++ stderr"
  puts stderr
  # @errors.push( stderr )
  puts! status, 'status'
  # @statuses.push( status )
end

#done_execObject



197
198
199
200
201
202
# File 'app/models/wco_hosting/serverhost.rb', line 197

def done_exec
  @messages ||= []
  @errors   ||= []
  @statuses ||= []
  OpenStruct.new( statuses: @statuses, errors: @errors, messages: @messages )
end

#ssh_hostObject

net-ssh, sshkit



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

field :ssh_host