<commit_before>require 'bundler'
Bundler.require
require 'json'
require './model/instance'
require './model/sshkey'
require './lib/dcmgr'

get '/' do
  haml :index
end

get '/instances/:id' do
  @instance = Instance[params[:id]]
  if @instance == nil
    {:status => "no instance"}.to_json
  else
    @instance.to_json
  end
end

get '/instances' do
  Instance.to_json
end

post '/testssh' do
  Dcmgr::ssh_connect
  {:status => "ssh connected"}.to_json
end

post '/instances/:name' do
  dm = Dcmgr.new('192.168.33.21', 'vagrant', 'vagrant')
  data = dm.launch_vm(params[:name])
  Instance.create(
  {
    name: params[:name],
    host_ip: '192.168.33.21',
    instance_path: data[:instance_path],
    created: Time.now,
    updated: Time.now
  })
  { status: 'instance created' }.to_json
end

delete '/instances/:id' do
  dm = Dcmgr.new('192.168.33.21', 'vagrant', 'vagrant')
  @instance = Instance[params[:id]]
  if @instance
    dm.remove_vm(@instance.name, @instance.instance_path)
    @instance.delete
    { status: 'instance removed' }.to_json
  else
    { status: 'no instance' }.to_json
  end
end

put '/instances/:id' do
  'instances updated'.to_json
end

get '/sshkeys/:id' do
  @sshkey = Sshkey[params[:id]]
  if @sshkey.nil?
    { status: 'no sshkey' }.to_json
  else
    @sshkey.to_json
  end
end

get '/sshkeys' do
  Sshkey.to_json
end

post '/sshkeys' do
  dm = Dcmgr.new('192.168.33.21', 'vagrant', 'vagrant')
  info = dm.generate_key('test')
  puts info.inspect
  Sshkey.create(
  {
    name: 'test',
    public_key: info[:result],
    created: Time.now,
    updated: Time.now
  })
  send_file "./#{info[:name]}", filename: info[:name], disposition: :attachment
end

put '/sshkeys/:id' do

end

delete '/sshkeys/:id' do
  @sshkey = Sshkey[params[:id]]<commit_msg>add configuration.<commit_after>require 'bundler'
Bundler.require
require 'json'
require './model/instance'
require './model/sshkey'
require './lib/dcmgr'

config_file 'config.yml'

get '/' do
  haml :index
end

get '/instances/:id' do
  @instance = Instance[params[:id]]
  if @instance == nil
    {:status => "no instance"}.to_json
  else
    @instance.to_json
  end
end

get '/instances' do
  Instance.to_json
end

post '/testssh' do
  Dcmgr::ssh_connect
  {:status => "ssh connected"}.to_json
end

post '/instances/:name' do
  dm = Dcmgr.new('192.168.33.21', 'vagrant', 'vagrant')
  data = dm.launch_vm(params[:name])
  Instance.create(
  {
    name: params[:name],
    host_ip: '192.168.33.21',
    instance_path: data[:instance_path],
    created: Time.now,
    updated: Time.now
  })
  { status: 'instance created' }.to_json
end

delete '/instances/:id' do
  dm = Dcmgr.new('192.168.33.21', 'vagrant', 'vagrant')
  @instance = Instance[params[:id]]
  if @instance
    dm.remove_vm(@instance.name, @instance.instance_path)
    @instance.delete
    { status: 'instance removed' }.to_json
  else
    { status: 'no instance' }.to_json
  end
end

put '/instances/:id' do
  'instances updated'.to_json
end

get '/sshkeys/:id' do
  @sshkey = Sshkey[params[:id]]
  if @sshkey.nil?
    { status: 'no sshkey' }.to_json
  else
    @sshkey.to_json
  end
end

get '/sshkeys' do
  Sshkey.to_json
end

post '/sshkeys' do
  dm = Dcmgr.new('192.168.33.21', 'vagrant', 'vagrant')
  info = dm.generate_key('test')
  Sshkey.create(
  {
    name: 'test',
    public_key: info[:result],
    created: Time.now,
    updated: Time.now
  })
  send_file "./#{info[:name]}", filename: info[:name], disposition: :attachment
end

put '/sshkeys/:id' do

end

delete '/sshkeys/:id' do
  @sshkey = Sshkey[params[:id]]
{'fraction_non_alphanumeric': 0.16503222191089942, 'fraction_numerical': 0.02241524236480807, 'mean_word_length': 3.518987341772152, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 8, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2514208', 'n_tokens_mistral': 1462, 'n_tokens_neox': 1387, 'n_words': 312}