Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create instances using custom configurations #4886

Conversation

QU3B1M
Copy link
Member

@QU3B1M QU3B1M commented Jan 29, 2024

Related issue
#4864

Description

This PR enables the usage of a custom configuration file to generate instances, this configuration file must be a .yaml that matches the specific ProviderConfig. E.g a custom vagrant config:

ip: '192.168.57.3'
cpu: 1
memory: 2048
box: 'generic/ubuntu2004'
box_version: '4.3.8'
public_key: '/tmp/wazuh-qa/VAGRANT-1179DAFB-EFDD-47BA-B97F-5ECC8B3428B7/instance_key.pub'

We can observe it matches the VagrantConfig model

class VagrantConfig(ProviderConfig):
    ip: str
    cpu: int
    memory: int
    box: str
    box_version: str
    public_key: str

Testing performed

Vagrant instance creation

  1. Create a .yaml file with the VagrantConfig values
    test.yaml
    ip: '192.168.57.3'
    cpu: 1
    memory: 2048
    box: 'generic/ubuntu2004'
    box_version: '4.3.8'
    public_key: '/tmp/wazuh-qa/VAGRANT-1179DAFB-EFDD-47BA-B97F-5ECC8B3428B7/instance_key.pub'
  2. Execute the allocator with the new parameter
    python launchers/allocation.py --provider vagrant --size small --composite-name linux-ubuntu-20.04-amd64  --custom-provider-config test.yaml
  3. Check track-output and the created Vagrantfile
    cat /tmp/wazuh-qa/track.yml
    identifier: VAGRANT-6B268EB6-3E12-454A-85EF-1B52548F4882
    instance_dir: /tmp/wazuh-qa/VAGRANT-6B268EB6-3E12-454A-85EF-1B52548F4882
    key_path: /tmp/wazuh-qa/VAGRANT-1179DAFB-EFDD-47BA-B97F-5ECC8B3428B7/instance_key.pub
    provider: vagrant
    /tmp/wazuh-qa/VAGRANT-6B268EB6-3E12-454A-85EF-1B52548F4882/Vagrantfile
    Vagrant.configure("2") do |config|
        config.vm.box = "generic/ubuntu2004"
        config.vm.box_version = "4.3.8"
        config.vm.provision "file", source: "/tmp/wazuh-qa/VAGRANT-1179DAFB-EFDD-47BA-B97F-5ECC8B3428B7/instance_key.pub", destination: ".ssh/authorized_keys"
        config.vm.network "private_network", ip:"192.168.57.3"
        config.vm.provider "virtualbox" do |v|
            v.memory = 2048
            v.cpus = 1
        end

AWS instance creation

Usage validation

  1. Create a .yaml file with the AWSConfig values
    test.yaml
    ami: 'ami-0fc5d935ebf8bc3bc'
    zone: 'us-east-1'
    user: 'ubuntu'
    key_name: 'AA86C4DBBF5A_key'
    type: 't2.small'
    security_groups: ['sg-023fd37c4e5b679de']
  2. Execute the allocator with the new parameter
    python launchers/allocation.py --provider aws --size small --composite-name linux-ubuntu-20.04-amd64  --custom-provider-config test.yaml
  3. Check track-output and the created instance on AWS
    cat /tmp/wazuh-qa/track.yml
    identifier: i-07cfa6cca8bda9628
    instance_dir: /tmp/wazuh-qa/i-07cfa6cca8bda9628
    key_path: /tmp/wazuh-qa/i-07cfa6cca8bda9628/AA86C4DBBF5A_key.pem
    provider: aws
    image

@QU3B1M QU3B1M self-assigned this Jan 29, 2024
@QU3B1M QU3B1M requested review from pro-akim and fcaffieri January 29, 2024 19:55
Copy link
Member

@pro-akim pro-akim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything LGTM, in case that the config file contains 1 host only.
Is this expected?

str: Identifier of the created instance.
"""
client = boto3.resource('ec2')
instance = client.create_instances(ImageId=config.ami,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be always 1 host?
Otherwise, [0] should be assigned by a variable

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the config is always "plain", in the Allocator class the function ___get_custom_config() validates the config comes with the correct format. In the case of a VagrantConfig, the config should be something like this

ip: '192.168.57.3'
cpu: 1
memory: 2048
box: 'generic/ubuntu2004'
box_version: '4.3.8'
public_key: '/tmp/wazuh-qa/VAGRANT-1179DAFB-EFDD-47BA-B97F-5ECC8B3428B7/instance_key.pub'

If the file has a list of hosts or something different, it will raise an error that the data on the file doesn't match with the model

Copy link
Member

@pro-akim pro-akim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Notes

LGTM

@fcaffieri fcaffieri merged commit b70eb2a into enhancement/4855-dtt1-iteration-3-allocator Jan 31, 2024
@fcaffieri fcaffieri deleted the enhancement/4864-dtt1-iteration-3-allocator-custom-configs branch January 31, 2024 13:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DTT1 - Iteration 3 - Allocation module - Enable custom VM creation from provider config
3 participants