Skip to content

Commit

Permalink
Make ssh_key more unique
Browse files Browse the repository at this point in the history
  • Loading branch information
JosepSampe committed Mar 9, 2023
1 parent 167f65e commit 1c85e50
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
8 changes: 4 additions & 4 deletions lithops/standalone/backends/aws_ec2/aws_ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def _load_ec2_data(self):
logger.debug(f'EC2 data loaded from {self.cache_file}')

if 'vpc_id' in self.ec2_data:
self.vpc_key = self.ec2_data['vpc_id'][-8:]
self.vpc_key = self.ec2_data['vpc_id'][-6:]
self.vpc_name = self.ec2_data['vpc_name']

def _dump_ec2_data(self):
Expand All @@ -116,7 +116,7 @@ def _create_vpc(self):
self.config['vpc_id'] = self.ec2_data['vpc_id']
return

self.vpc_name = self.config.get('vpc_name', f'lithops-vpc-{self.user_key}-{str(uuid.uuid4())[-8:]}')
self.vpc_name = self.config.get('vpc_name', f'lithops-vpc-{self.user_key}-{str(uuid.uuid4())[-6:]}')
logger.debug(f'Setting VPC name to: {self.vpc_name}')

assert re.match("^[a-z0-9-:-]*$", self.vpc_name),\
Expand Down Expand Up @@ -252,7 +252,7 @@ def _create_ssh_key(self):
self.config['ssh_key_filename'] = self.ec2_data['ssh_key_filename']
return

keyname = f'lithops-key-{self.vpc_key}'
keyname = f'lithops-key-{str(uuid.uuid4())[-8:]}'
filename = os.path.join("~", ".ssh", f"{keyname}.{self.name}.id_rsa")
key_filename = os.path.expanduser(filename)

Expand Down Expand Up @@ -363,7 +363,7 @@ def init(self):
self._create_vpc()

# Set the suffix used for the VPC resources
self.vpc_key = self.config['vpc_id'][-8:]
self.vpc_key = self.config['vpc_id'][-6:]

# Create the internet gateway if not exists
self. _create_internet_gateway()
Expand Down
31 changes: 5 additions & 26 deletions lithops/standalone/backends/ibm_vpc/ibm_vpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def _load_vpc_data(self):
logger.debug(f'VPC data loaded from {self.cache_file}')

if 'vpc_id' in self.vpc_data:
self.vpc_key = self.vpc_data['vpc_id'][-8:]
self.vpc_key = self.vpc_data['vpc_id'][-6:]
self.vpc_name = self.vpc_data['vpc_name']

return self.vpc_data
Expand Down Expand Up @@ -121,7 +121,7 @@ def _create_vpc(self):
vpc_info = None

iam_id = self.iam_api_key[:4].lower()
self.vpc_name = self.config.get('vpc_name', f'lithops-vpc-{iam_id}-{str(uuid.uuid4())[-8:]}')
self.vpc_name = self.config.get('vpc_name', f'lithops-vpc-{iam_id}-{str(uuid.uuid4())[-6:]}')
logger.debug(f'Setting VPC name to: {self.vpc_name}')

assert re.match("^[a-z0-9-:-]*$", self.vpc_name),\
Expand Down Expand Up @@ -193,7 +193,7 @@ def _create_ssh_key(self):
except ApiException:
pass

keyname = f'lithops-key-{self.vpc_key}'
keyname = f'lithops-key-{str(uuid.uuid4())[-8:]}'
filename = os.path.join("~", ".ssh", f"{keyname}.{self.name}.id_rsa")
key_filename = os.path.expanduser(filename)

Expand Down Expand Up @@ -440,7 +440,7 @@ def init(self):
self._create_vpc()

# Set the suffix used for the VPC resources
self.vpc_key = self.config['vpc_id'][-8:]
self.vpc_key = self.config['vpc_id'][-6:]

# Create the ssh key pair if not exists
self._create_ssh_key()
Expand Down Expand Up @@ -631,13 +631,6 @@ def _delete_subnet(self):
Deletes all VM instances in the VPC
"""
subnet_name = f'lithops-subnet-{self.vpc_key}'
if 'subnet_id' not in self.vpc_data:
subnets_info = self.vpc_cli.list_subnets().get_result()

for subn in subnets_info['subnets']:
if subn['name'] == subnet_name:
self.vpc_data['subnet_id'] = subn['id']

if 'subnet_id' in self.vpc_data:
logger.debug(f'Deleting subnet {subnet_name}')

Expand All @@ -662,13 +655,6 @@ def _delete_gateway(self):
Deletes the public gateway
"""
gateway_name = f'lithops-gateway-{self.vpc_key}'
if 'gateway_id' not in self.vpc_data:
gateways_info = self.vpc_cli.list_public_gateways().get_result()

for gw in gateways_info['public_gateways']:
if ['name'] == gateway_name:
self.vpc_data['gateway_id'] = gw['id']

if 'gateway_id' in self.vpc_data:
logger.debug(f'Deleting gateway {gateway_name}')
try:
Expand All @@ -686,19 +672,12 @@ def _delete_vpc(self):
if self.vpc_data['vpc_data_type'] == 'provided':
return

msg = (f'Deleting all Lithops VPC resources from {self.vpc_name}'
if self.vpc_name else 'Deleting all Lithops VPC resources')
msg = (f'Deleting all Lithops VPC resources from {self.vpc_name}')
logger.info(msg)

self._delete_subnet()
self._delete_gateway()

if 'vpc_id' not in self.vpc_data:
vpcs_info = self.vpc_cli.list_vpcs().get_result()
for vpc in vpcs_info['vpcs']:
if vpc['name'] == self.vpc_name:
self.vpc_data['vpc_id'] = vpc['id']

if 'vpc_id' in self.vpc_data:
logger.debug(f'Deleting VPC {self.vpc_data["vpc_name"]}')
try:
Expand Down
33 changes: 30 additions & 3 deletions runtime/ibm_vpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,35 @@

In IBM VPC, you can run functions by using a Virtual machine (VM). In the VM, functions run using parallel processes. In this case, it is not needed to install anything in the remote VMs since Lithops does this process automatically the first time you use them. However, use a custom VM it is a preferable approach, since using a pre-built custom image will greatly improve the overall execution time. To benefit from this approach, follow the following steps:

## Build the custom image
For building the VM image that contains all dependencies required by Lithops, execute the [build script](build_lithops_runtime.sh) located in this folder. The best is to use vanilla Ubuntu machine to run this script and this script will use a base image based on **ubuntu-20.04-server-cloudimg-amd64**. There is need to have sudo privileges to run this script. We advice to create a new VSI in VPC with minimal setup, like `cx2-2x4`, setup floating IP for this machine and use it to build custom image. Once you accessed the machine, download the script
## Option 1:
For building the default VM image that contains all dependencies required by Lithops, execute:

```
lithops image build -b ibm_vpc
```

This command will create an image called "lithops-worker-default" in the target region.
If the image already exists, and you want to updete it, use the '--overwrite' or '-o' flag:

```
lithops image build -b ibm_vpc --overwrite
```

Note that if you want to use this default image, there is no need to provide the image ID in the config, since Lithops will automatically look for it.

For creating a custom VM image, you can provide an `.sh` script with all the desired commands as an input of the previous command, and you can also provide a custom name:

```
lithops image build -b ibm_vpc -f myscript.sh custom-lithops-runtime
```

In this case, if you use a custom name, you must provide the Image ID printed at the end of the build command in your lithops config.


## Option 2:

For building the VM image that contains all dependencies required by Lithops, execute the [build script](build_lithops_runtime.sh) located in this folder. The best is to use vanilla Ubuntu machine to run this script and this script will use a base image based on **ubuntu-20.04-server-cloudimg-amd64**. There is need to have sudo privileges to run this script.
Once you accessed the machine, download the script

wget https://raw.githubusercontent.com/lithops-cloud/lithops/master/runtime/ibm_vpc/build_lithops_vm_image.sh

Expand Down Expand Up @@ -37,7 +64,7 @@ Alternative is to build a VM image without a Docker runtime. This approach is ma
In this example the script generates a VM image named `lithops-ubuntu-20.04.qcow2` that contains all dependencies required by Lithops.


## Deploy the image
### Deploy the image

Once local image is ready you need to upload it to COS. The best would be to use the `lithops storage` CLI:

Expand Down

0 comments on commit 1c85e50

Please sign in to comment.