Skip to content
Permalink
Browse files

add api, github issue #98

  • Loading branch information...
Raekye committed Aug 11, 2019
1 parent ee08a71 commit c5fd7b4632b398f7159ce4191ffed3e29aedae9f
@@ -254,6 +254,94 @@ def clear_logs
return redirect_to server_path(@server), flash: { success: 'Server logs cleared' }
end

def api_status
server = Server.where(id: params[:id], api_key: params[:key])
if server.length == 0
render json: {
error: 'Not found',
}, status: 404
return
end
active = server[0].running?
status = server[0].pending_operation
minecraft = server[0].minecraft.running?
render json: {
server: active,
status: status,
minecraft: minecraft,
}
end

def api_start
server = Server.where(id: params[:id], api_key: params[:key])
if server.length == 0
render json: {
error: 'Not found',
}, status: 404
return
end
err = server[0].start
return render json: {
error: err,
}
end

def api_stop
server = Server.where(id: params[:id], api_key: params[:key])
if server.length == 0
render json: {
error: 'Not found',
}, status: 404
return
end
err = server[0].stop
return render json: {
error: err,
}
end

def api_reboot
server = Server.where(id: params[:id], api_key: params[:key])
if server.length == 0
render json: {
error: 'Not found',
}, status: 404
return
end
err = server[0].reboot
return render json: {
error: err,
}
end

def api_pause
server = Server.where(id: params[:id], api_key: params[:key])
if server.length == 0
render json: {
error: 'Not found',
}, status: 404
return
end
err = server[0].minecraft.pause
return render json: {
error: err,
}
end

def api_resume
server = Server.where(id: params[:id], api_key: params[:key])
if server.length == 0
render json: {
error: 'Not found',
}, status: 404
return
end
err = server[0].minecraft.resume
return render json: {
error: err,
}
end

def show_digital_ocean_droplets
@do_droplets = current_user.digital_ocean_droplets
render layout: nil
@@ -382,6 +470,7 @@ def server_advanced_params
:remote_snapshot_id,
:remote_region_slug,
:remote_size_slug,
:api_key,
minecraft_attributes: [:mcsw_password],
)
end
@@ -2,7 +2,7 @@
#
# Table name: minecrafts
#
# id :integer not null, primary key
# id :bigint not null, primary key
# created_at :datetime
# updated_at :datetime
# server_id :uuid not null
@@ -2,7 +2,7 @@
#
# Table name: scheduled_tasks
#
# id :integer not null, primary key
# id :bigint not null, primary key
# server_id :uuid not null
# partition :integer not null
# action :string not null
@@ -17,6 +17,7 @@
# remote_size_slug :string not null
# remote_snapshot_id :integer
# timezone_delta :integer default(0), not null
# api_key :string not null
#

class Server < ActiveRecord::Base
@@ -45,6 +46,7 @@ class Server < ActiveRecord::Base
def after_initialize_callback
chars = ('a'..'z').to_a
self.domain ||= (0...8).map { chars[rand(chars.length)] }.join
self.api_key ||= SecureRandom.hex(16)
end

def before_validate_callback
@@ -55,6 +57,7 @@ def before_validate_callback
self.remote_region_slug = self.remote_region_slug.clean
self.remote_size_slug = self.remote_size_slug.clean
self.ssh_keys = self.ssh_keys.try(:gsub, /\s/, '').clean
self.api_key = self.api_key.clean
end

def schedule_text
@@ -2,7 +2,7 @@
#
# Table name: server_logs
#
# id :integer not null, primary key
# id :bigint not null, primary key
# server_id :uuid not null
# message :text not null
# debuginfo :string(255) not null
@@ -2,7 +2,7 @@
#
# Table name: users
#
# id :integer not null, primary key
# id :bigint not null, primary key
# email :string(255) default(""), not null
# encrypted_password :string(255) default(""), not null
# reset_password_token :string(255)
@@ -8,6 +8,7 @@
<%= f.input :remote_snapshot_id, label: 'Digital Ocean saved snapshot id' %>
<%= f.input :remote_region_slug, label: 'Digital Ocean region slug' %>
<%= f.input :remote_size_slug, label: 'Digital Ocean size slug' %>
<%= f.input :api_key, label: 'API key' %>
<%= f.simple_fields_for :minecraft do |f_m| %>
<%= f_m.input :mcsw_password, label: 'MCSW key', as: :string %>
<% end %>
@@ -49,7 +49,7 @@ def perform(server_id, times = 0)
host.key = Gamocosm::DIGITAL_OCEAN_SSH_PRIVATE_KEY_PATH
host.ssh_options = {
passphrase: Gamocosm::DIGITAL_OCEAN_SSH_PRIVATE_KEY_PASSPHRASE,
verify_host_key: false,
verify_host_key: :never,
# how long to wait for initial connection
# `e.cause` will be `Timeout::Error`
timeout: 4
@@ -89,6 +89,12 @@
get 'autoshutdown_enable'
get 'autoshutdown_disable'
get 'clear_logs'
get 'api/:key/status', to: 'servers#api_status', as: :api_status
get 'api/:key/start', to: 'servers#api_start', as: :api_start
get 'api/:key/stop', to: 'servers#api_stop', as: :api_stop
get 'api/:key/reboot', to: 'servers#api_reboot', as: :api_reboot
get 'api/:key/pause', to: 'servers#api_pause', as: :api_pause
get 'api/:key/resume', to: 'servers#api_resume', as: :api_resume
end
end

@@ -0,0 +1,17 @@
class AddApiKeyToServers < ActiveRecord::Migration[5.2]
def change
add_column :servers, :api_key, :string

reversible do |dir|
dir.up do
Server.all.each do |s|
s.api_key = SecureRandom.hex(16)
s.save
end
change_column :servers, :api_key, :string, { null: false }
end
dir.down do
end
end
end
end
@@ -1,4 +1,3 @@
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
@@ -11,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20151106024157) do
ActiveRecord::Schema.define(version: 2019_08_09_174600) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -20,83 +19,78 @@
create_table "minecrafts", force: :cascade do |t|
t.datetime "created_at"
t.datetime "updated_at"
t.uuid "server_id", null: false
t.string "flavour", null: false
t.string "mcsw_password", null: false
t.boolean "autoshutdown_enabled", default: false, null: false
t.datetime "autoshutdown_last_check", null: false
t.datetime "autoshutdown_last_successful", null: false
t.integer "autoshutdown_minutes", default: 8, null: false
t.uuid "server_id", null: false
t.string "flavour", null: false
t.string "mcsw_password", null: false
t.boolean "autoshutdown_enabled", default: false, null: false
t.datetime "autoshutdown_last_check", null: false
t.datetime "autoshutdown_last_successful", null: false
t.integer "autoshutdown_minutes", default: 8, null: false
t.index ["server_id"], name: "index_minecrafts_on_server_id", unique: true
end

add_index "minecrafts", ["server_id"], name: "index_minecrafts_on_server_id", unique: true, using: :btree

create_table "scheduled_tasks", force: :cascade do |t|
t.uuid "server_id", null: false
t.uuid "server_id", null: false
t.integer "partition", null: false
t.string "action", null: false
t.string "action", null: false
t.index ["partition"], name: "index_scheduled_tasks_on_partition"
end

add_index "scheduled_tasks", ["partition"], name: "index_scheduled_tasks_on_partition", using: :btree

create_table "server_logs", force: :cascade do |t|
t.uuid "server_id", null: false
t.text "message", null: false
t.string "debuginfo", limit: 255, null: false
t.uuid "server_id", null: false
t.text "message", null: false
t.string "debuginfo", limit: 255, null: false
t.datetime "created_at"
t.datetime "updated_at"
t.index ["server_id"], name: "index_server_logs_on_server_id"
end

add_index "server_logs", ["server_id"], name: "index_server_logs_on_server_id", using: :btree

create_table "servers", id: :uuid, default: "uuid_generate_v4()", force: :cascade do |t|
t.integer "user_id", null: false
t.string "name", limit: 255, null: false
create_table "servers", id: :uuid, default: -> { "uuid_generate_v4()" }, force: :cascade do |t|
t.integer "user_id", null: false
t.string "name", limit: 255, null: false
t.datetime "created_at"
t.datetime "updated_at"
t.string "domain", null: false
t.string "pending_operation"
t.integer "ssh_port", default: 4022, null: false
t.string "ssh_keys"
t.integer "setup_stage", default: 0, null: false
t.integer "remote_id"
t.string "remote_region_slug", null: false
t.string "remote_size_slug", null: false
t.integer "remote_snapshot_id"
t.integer "timezone_delta", default: 0, null: false
t.string "domain", null: false
t.string "pending_operation"
t.integer "ssh_port", default: 4022, null: false
t.string "ssh_keys"
t.integer "setup_stage", default: 0, null: false
t.integer "remote_id"
t.string "remote_region_slug", null: false
t.string "remote_size_slug", null: false
t.integer "remote_snapshot_id"
t.integer "timezone_delta", default: 0, null: false
t.string "api_key", null: false
t.index ["domain"], name: "index_servers_on_domain", unique: true
t.index ["user_id"], name: "index_servers_on_user_id"
end

add_index "servers", ["domain"], name: "index_servers_on_domain", unique: true, using: :btree
add_index "servers", ["user_id"], name: "index_servers_on_user_id", using: :btree

create_table "servers_users", force: :cascade do |t|
t.uuid "server_id"
t.uuid "server_id"
t.integer "user_id"
t.index ["server_id", "user_id"], name: "index_servers_users_on_server_id_and_user_id", unique: true
t.index ["server_id"], name: "index_servers_users_on_server_id"
t.index ["user_id"], name: "index_servers_users_on_user_id"
end

add_index "servers_users", ["server_id", "user_id"], name: "index_servers_users_on_server_id_and_user_id", unique: true, using: :btree
add_index "servers_users", ["server_id"], name: "index_servers_users_on_server_id", using: :btree
add_index "servers_users", ["user_id"], name: "index_servers_users_on_user_id", using: :btree

create_table "users", force: :cascade do |t|
t.string "email", limit: 255, default: "", null: false
t.string "encrypted_password", limit: 255, default: "", null: false
t.string "reset_password_token", limit: 255
t.string "email", limit: 255, default: "", null: false
t.string "encrypted_password", limit: 255, default: "", null: false
t.string "reset_password_token", limit: 255
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip", limit: 255
t.string "last_sign_in_ip", limit: 255
t.string "current_sign_in_ip", limit: 255
t.string "last_sign_in_ip", limit: 255
t.datetime "created_at"
t.datetime "updated_at"
t.string "digital_ocean_api_key", limit: 255
t.string "digital_ocean_api_key", limit: 255
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end

add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree

add_foreign_key "minecrafts", "servers", on_delete: :cascade
add_foreign_key "scheduled_tasks", "servers", on_delete: :cascade
add_foreign_key "server_logs", "servers", on_delete: :cascade
@@ -2,7 +2,7 @@
#
# Table name: minecrafts
#
# id :integer not null, primary key
# id :bigint not null, primary key
# created_at :datetime
# updated_at :datetime
# server_id :uuid not null
@@ -2,7 +2,7 @@
#
# Table name: scheduled_tasks
#
# id :integer not null, primary key
# id :bigint not null, primary key
# server_id :uuid not null
# partition :integer not null
# action :string not null
@@ -17,6 +17,7 @@
# remote_size_slug :string not null
# remote_snapshot_id :integer
# timezone_delta :integer default(0), not null
# api_key :string not null
#

require 'test_helper'
@@ -2,7 +2,7 @@
#
# Table name: users
#
# id :integer not null, primary key
# id :bigint not null, primary key
# email :string(255) default(""), not null
# encrypted_password :string(255) default(""), not null
# reset_password_token :string(255)

0 comments on commit c5fd7b4

Please sign in to comment.
You can’t perform that action at this time.