mutliple modif

main
stef 2024-04-17 15:23:21 +02:00
parent 4de8394440
commit f28150b9f5
33 changed files with 430 additions and 6 deletions

View File

@ -70,3 +70,5 @@ gem "bulma-rails", "~> 0.9.4"
gem "jquery-rails", "~> 4.6"
gem "devise", "~> 4.9"
gem "securerandom", "~> 0.3.1"

View File

@ -235,6 +235,7 @@ GEM
rubyzip (2.3.2)
sassc (2.4.0)
ffi (~> 1.9)
securerandom (0.3.1)
selenium-webdriver (4.17.0)
base64 (~> 0.2)
rexml (~> 3.2, >= 3.2.5)
@ -295,6 +296,7 @@ DEPENDENCIES
mongoid (~> 8.1)
puma (>= 5.0)
rails (~> 7.1.3)
securerandom (~> 0.3.1)
selenium-webdriver
sprockets-rails
stimulus-rails

View File

@ -0,0 +1,70 @@
class AnshostsController < ApplicationController
before_action :set_anshost, only: %i[ show edit update destroy ]
# GET /anshosts or /anshosts.json
def index
@anshosts = Anshost.all
end
# GET /anshosts/1 or /anshosts/1.json
def show
end
# GET /anshosts/new
def new
@anshost = Anshost.new
end
# GET /anshosts/1/edit
def edit
end
# POST /anshosts or /anshosts.json
def create
@anshost = Anshost.new(anshost_params)
respond_to do |format|
if @anshost.save
format.html { redirect_to anshost_url(@anshost), notice: "Anshost was successfully created." }
format.json { render :show, status: :created, location: @anshost }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @anshost.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /anshosts/1 or /anshosts/1.json
def update
respond_to do |format|
if @anshost.update(anshost_params)
format.html { redirect_to anshost_url(@anshost), notice: "Anshost was successfully updated." }
format.json { render :show, status: :ok, location: @anshost }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @anshost.errors, status: :unprocessable_entity }
end
end
end
# DELETE /anshosts/1 or /anshosts/1.json
def destroy
@anshost.destroy!
respond_to do |format|
format.html { redirect_to anshosts_url, notice: "Anshost was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_anshost
@anshost = Anshost.find(params[:id])
end
# Only allow a list of trusted parameters through.
def anshost_params
params.require(:anshost).permit(:hostname, :var)
end
end

View File

@ -134,6 +134,6 @@ class HostsController < ApplicationController
# Only allow a list of trusted parameters through.
def host_params
params.require(:host).permit(:uuid, :hostname, :ip, :status, :mac, :discover, :installed, :interface , :installtemplate, :site , :installtemplate_id, :site_id, :rootaccount_id, :mainaccount_id,:toinstall, :installip)
params.require(:host).permit(:uuid, :hostname, :ip, :status, :mac, :discover, :installed, :interface , :installtemplate, :site , :installtemplate_id, :site_id, :rootaccount_id, :mainaccount_id,:toinstall, :installip,:luskkey)
end
end

View File

@ -0,0 +1,24 @@
class TestController < ApplicationController
def myip
@host = Host.new()
sourceip = client_ip
uuid="uuid-#{sourceip}"
hostname="new-#{sourceip}"
@template = Installtemplate.find_by(name:"Test Ubuntu - Desktop - nfs")
@site = Site.find_by(name:"default")
@account = Account.find_by(name:"ansible")
randomstring = SecureRandom.hex(12)
@host = Host.new(hostname: hostname,uuid: uuid, installip: sourceip, installtemplate: @template,site: @site, mac: "00:00:00:00:00:00", status: "discover", discover: true,installed: false, toinstall: false,rootaccount: @account, mainaccount: @account, luskkey: randomstring )
@host.save!
string = "You IP address is #{sourceip}"
render plain: string
end
def create
end
private
def client_ip
request.remote_ip
end
end

View File

@ -0,0 +1 @@
{"var1":1,"var2":2,"sub":{"var1":3,"var2":4},"var3":3}

View File

@ -0,0 +1,10 @@
---
:title: Coffee
:ingredients:
- :ingredient_name: Water
:quantity: 8
:unit_of_measurement: oz
- :ingredient_name: Coffee Grounds
:quantity: 1
:unit_of_measurement: tbsp
:directions: Make coffee.

View File

@ -0,0 +1,2 @@
module AnshostsHelper
end

View File

@ -0,0 +1,16 @@
class Filejson
def initialize(path)
@path = path
file = File.read(@path)
@content = JSON.parse(file)
end
def get
@content
end
def save
File.open(@path, 'w') do |f|
f.write(@content.to_json)
end
end
end

View File

@ -0,0 +1,13 @@
class Fileyaml
def initialize(path)
@path = path
@content = YAML.load_file(@path)
end
def get
@content
end
def save
File.open(@path, "w") { |file| file.write(@content.to_yaml) }
end
end

View File

@ -0,0 +1,6 @@
class Anshost
include Mongoid::Document
include Mongoid::Timestamps
field :hostname, type: String
field :var, type: Hash
end

View File

@ -14,6 +14,7 @@ class Host
field :lastbootgenerated, type: String , default: ''
field :lastinstallgenerated, type: String , default: ''
field :lastpostinstallgenerated, type: String, default: ''
field :luskkey, type: String, default: '123456789123456789'
belongs_to :installtemplate , class_name: "Installtemplate", inverse_of: :hostreferences
belongs_to :site , class_name: "Site", inverse_of: :sitereferences
belongs_to :rootaccount , class_name: "Account", inverse_of: :rootref

View File

@ -0,0 +1,12 @@
<div id="<%= dom_id anshost %>">
<p>
<strong>Hostname:</strong>
<%= anshost.hostname %>
</p>
<p>
<strong>Var:</strong>
<%= anshost.var %>
</p>
</div>

View File

@ -0,0 +1,2 @@
json.extract! anshost, :id, :hostname, :var, :created_at, :updated_at
json.url anshost_url(anshost, format: :json)

View File

@ -0,0 +1,28 @@
<%= form_with(model: anshost) do |form| %>
<% if anshost.errors.any? %>
<div style="color: red">
<h2><%= pluralize(anshost.errors.count, "error") %> prohibited this anshost from being saved:</h2>
<ul>
<% anshost.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div>
<%= form.label :hostname, style: "display: block" %>
<%= form.text_field :hostname %>
</div>
<% anshost.var.keys.each do |key| %>
<div>
<%= form.label key, style: "display: block" %>
<%= form.text_field anshost.var[key] %>
</div>
<% end %>
<div>
<%= form.submit %>
</div>
<% end %>

View File

@ -0,0 +1,10 @@
<h1>Editing anshost</h1>
<%= render "form", anshost: @anshost %>
<br>
<div>
<%= link_to "Show this anshost", @anshost %> |
<%= link_to "Back to anshosts", anshosts_path %>
</div>

View File

@ -0,0 +1,14 @@
<p style="color: green"><%= notice %></p>
<h1>Anshosts</h1>
<div id="anshosts">
<% @anshosts.each do |anshost| %>
<%= render anshost %>
<p>
<%= link_to "Show this anshost", anshost %>
</p>
<% end %>
</div>
<%= link_to "New anshost", new_anshost_path %>

View File

@ -0,0 +1 @@
json.array! @anshosts, partial: "anshosts/anshost", as: :anshost

View File

@ -0,0 +1,9 @@
<h1>New anshost</h1>
<%= render "form", anshost: @anshost %>
<br>
<div>
<%= link_to "Back to anshosts", anshosts_path %>
</div>

View File

@ -0,0 +1,10 @@
<p style="color: green"><%= notice %></p>
<%= render @anshost %>
<div>
<%= link_to "Edit this anshost", edit_anshost_path(@anshost) %> |
<%= link_to "Back to anshosts", anshosts_path %>
<%= button_to "Destroy this anshost", @anshost, method: :delete %>
</div>

View File

@ -0,0 +1 @@
json.partial! "anshosts/anshost", anshost: @anshost

View File

@ -92,6 +92,19 @@
</div>
</div>
<div class="field is-horizontal">
<div class="field-label is-small">
<label class="label">Lusk Key</label>
</div>
<div class="field-body">
<div class="field">
<div class="control">
<%= form.text_field :luskkey , { class: "input"} %>
</div>
</div>
</div>
</div>
<div class="field is-horizontal">
<div class="field-label is-small">
<label class="label">Interface</label>
@ -145,6 +158,7 @@
<%= form.collection_select :mainaccount_id, Account.all, :id, :name %>
</div>
</div>
<div class="control">
<%= form.label :discover, class: "checkbox" %>
@ -162,9 +176,11 @@
<div class="control">
<button class="button is-link">Submit</button>
<%= link_to "Return", "/hosts", class: "button is-success" %>
<%= button_to "Destroy", @host, method: :delete, class: "button is-danger is-pulled-right" %>
</div>
<% end %>
<%= button_to " Destroy ", @host, method: :delete, class: "button is-danger" %>
</div>
</div>
</div>

View File

@ -108,5 +108,4 @@
</div>
<% end %>
<%= button_to " Destroy ", @installtemplate, method: :delete, class: "button is-danger" %>

View File

@ -8,7 +8,8 @@
<%= link_to "Scripts" , scripts_path %>
<%= link_to "Templates" , installtemplates_path %>
<%= link_to "Sites" , sites_path %>
<%= link_to "Account" , accounts_path %>
<%= link_to "Account" , accounts_path %>
<%= link_to "Inventory" , anshosts_path %>
</ul>
</li>

View File

@ -64,4 +64,6 @@ Rails.application.configure do
# Raise error when a before_action's only/except options reference missing actions
config.action_controller.raise_on_missing_callback_actions = true
config.web_console.whitelisted_ips = '0.0.0.0/0'
end

View File

@ -1,4 +1,5 @@
Rails.application.routes.draw do
resources :anshosts
devise_for :users
resources :scripts
resources :sites
@ -33,6 +34,6 @@ Rails.application.routes.draw do
get '/api/host/cloudinit/:uuid/meta-data', to: "engine#generate_metadata", defaults: { format: 'text' }
get '/api/host/cloudinit/:uuid/vendor-data', to: "engine#generate_metadata", defaults: { format: 'text' }
# test
get '/test/myip', to: "test#myip", defaults: { format: 'text' }
end

View File

@ -0,0 +1,64 @@
#cloud-config
autoinstall:
version: 1
packages:
- ubuntu-desktop
snaps:
- name: firefox
- name: gnome-3-38-2004
- name: gtk-common-themes
- name: snap-store
- name: snapd-desktop-integration
interactive-sections:
- identity
keyboard:
layout: fr
toggle: null
variant: ''
locale: fr_FR.UTF-8
storage:
layout:
name: lvm
password: touch
ssh:
install-server: true
# option "allow-pw" defaults to `true` if authorized_keys is empty, `false` otherwise.
allow-pw: false
updates: security
# Ubuntu Desktop uses the hwe flavor kernel by default.
early-commands:
- echo 'linux-generic-hwe-22.04' > /run/kernel-meta-package
# "[late-commands] are run in the installer environment with the installed system mounted at /target."
late-commands:
# randomly generate the hostname & show the IP at boot
- echo ubuntu-host-$(openssl rand -hex 3) > /target/etc/hostname
# dump the IP out at login screen
- echo "Ubuntu 22.04 LTS \nIP - $(hostname -I)\n" > /target/etc/issue
# Let NetworkManager handle network
- rm /target/etc/netplan/00-installer-config*yaml
- >-
printf "network:\n version: 2\n renderer: NetworkManager"
> /target/etc/netplan/01-network-manager-all.yaml
# shut-down the host to avoid an infinite installer loop
- shutdown -h now
user-data:
disable_root: true
timezone: Europe/Paris
package_upgrade: false
users:
- name: useritlu
primary_group: users
groups: sudo
lock_passwd: false
# don't need PW since using SSH, leaving this in though...
# password is "changeme" - created with `docker run -it --rm alpine mkpasswd --method=SHA-512`
passwd: "$5$IWwNqL9VUSDoc4Jv$DEUGR.cZQcbz/QvdCOmU13fX5ZW0rANg8LqkAtX3nBA"
shell: /bin/bash
# use cat ~/.ssh/id_rsa.pub or generate to get your public key
ssh_authorized_keys:
- "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC14slyp/JGv9iqLH4D94x+7v4PB/ec0YbLUPexdBip6OJaflbmp3s25WJ+oyO6U78Ee0jZUZt0TapYozyNx9UksP9JhirwKeNJnQSzSX0RKc6kQffoCgWHZmnzuoalEzaE7XyH+K8wP+hKi052ak9yR7XWDp6CG3V1Qpyq80VD1XNUzEL2xkITGQ6KojxrOJ1O0A9ISRu1t85Ul2N0syIylE2Ukvns1/NkArhC2g8N8T5XxPq39AUH78A3I0/kHowIzW9BpPVwim0tJTLSVNnVqq1NPG+gi1XvrXKzO/jb4kT01tnMG9vKcYqdH4g0y01ADEcCgMo1jGAjwq6gPLqT imported-openssh-key"
sudo: ALL=(ALL) NOPASSWD:ALL
# shutdown after first host initial provisioning
power_state:
mode: poweroff

View File

@ -0,0 +1,48 @@
require "test_helper"
class AnshostsControllerTest < ActionDispatch::IntegrationTest
setup do
@anshost = anshosts(:one)
end
test "should get index" do
get anshosts_url
assert_response :success
end
test "should get new" do
get new_anshost_url
assert_response :success
end
test "should create anshost" do
assert_difference("Anshost.count") do
post anshosts_url, params: { anshost: { hostname: @anshost.hostname, var: @anshost.var } }
end
assert_redirected_to anshost_url(Anshost.last)
end
test "should show anshost" do
get anshost_url(@anshost)
assert_response :success
end
test "should get edit" do
get edit_anshost_url(@anshost)
assert_response :success
end
test "should update anshost" do
patch anshost_url(@anshost), params: { anshost: { hostname: @anshost.hostname, var: @anshost.var } }
assert_redirected_to anshost_url(@anshost)
end
test "should destroy anshost" do
assert_difference("Anshost.count", -1) do
delete anshost_url(@anshost)
end
assert_redirected_to anshosts_url
end
end

View File

@ -0,0 +1,9 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
hostname: MyString
var:
two:
hostname: MyString
var:

View File

@ -0,0 +1,7 @@
require "test_helper"
class AnshostTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View File

@ -0,0 +1,43 @@
require "application_system_test_case"
class AnshostsTest < ApplicationSystemTestCase
setup do
@anshost = anshosts(:one)
end
test "visiting the index" do
visit anshosts_url
assert_selector "h1", text: "Anshosts"
end
test "should create anshost" do
visit anshosts_url
click_on "New anshost"
fill_in "Hostname", with: @anshost.hostname
fill_in "Var", with: @anshost.var
click_on "Create Anshost"
assert_text "Anshost was successfully created"
click_on "Back"
end
test "should update Anshost" do
visit anshost_url(@anshost)
click_on "Edit this anshost", match: :first
fill_in "Hostname", with: @anshost.hostname
fill_in "Var", with: @anshost.var
click_on "Update Anshost"
assert_text "Anshost was successfully updated"
click_on "Back"
end
test "should destroy Anshost" do
visit anshost_url(@anshost)
click_on "Destroy this anshost", match: :first
assert_text "Anshost was successfully destroyed"
end
end