Skip to content

Commit

Permalink
Enable regions (#45)
Browse files Browse the repository at this point in the history
* Add enable-regions command

This enables all regions that are disabled. This is useful when you
want to upload an AMI to all regions, but some regions are disabled.

* Expose all pyproject scripts as apps

* quota increase only when  change detected
  • Loading branch information
arianvp authored Jan 8, 2024
1 parent 5357737 commit a9f28b5
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 28 deletions.
21 changes: 7 additions & 14 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,13 @@
});

apps = lib.genAttrs self.lib.supportedSystems (system:
let pkgs = nixpkgs.legacyPackages.${system}; in {
nuke = {
type = "app";
program = "${self.packages.${system}.upload-ami}/bin/nuke";
};
smoke-test = {
type = "app";
program = "${self.packages.${system}.upload-ami}/bin/smoke-test";
};
disable-image-block-public-access = {
type = "app";
program = "${self.packages.${system}.upload-ami}/bin/disable-image-block-public-access";
};
});
let
pkgs = nixpkgs.legacyPackages.${system};
upload-ami = self.packages.${system}.upload-ami;
mkApp = name: _: { type = "app"; program = "${upload-ami}/bin/${name}"; };
in
lib.mapAttrs mkApp self.packages.${system}.upload-ami.passthru.pyproject.project.scripts
);


# TODO: unfortunately I don't have access to a aarch64-linux hardware with virtualisation support
Expand Down
2 changes: 2 additions & 0 deletions upload-ami/default.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{ buildPythonApplication
, python3Packages
, lib
}:

let pyproject = builtins.fromTOML (builtins.readFile ./pyproject.toml);
Expand All @@ -16,4 +17,5 @@ buildPythonApplication {
propagatedBuildInputs =
map (name: python3Packages.${name}) pyproject.project.dependencies;

passthru.pyproject = pyproject;
}
4 changes: 3 additions & 1 deletion upload-ami/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ dependencies = [
upload-ami = "upload_ami.cli:main"
nuke = "upload_ami.nuke:main"
smoke-test = "upload_ami.smoke_test:main"
disable-image-block-public-access = "upload_ami.disable_image_block_public_access:main"
disable-image-block-public-access = "upload_ami.disable_image_block_public_access:main"
enable-regions = "upload_ami.enable_regions:main"
request-public-ami-quota-increase = "upload_ami.request_public_ami_quota_increase:main"
19 changes: 19 additions & 0 deletions upload-ami/src/upload_ami/enable_regions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import boto3
import logging


def main():
"""
Enable all regions that are disabled
Due to rate limiting, you might need to run this multiple times.
"""
logging.basicConfig(level=logging.INFO)
account = boto3.client("account")
pages = account.get_paginator("list_regions").paginate(
RegionOptStatusContains=["DISABLED"]
)
for page in pages:
for region in page["Regions"]:
logging.info(f"enabling region {region['RegionName']}")
account.enable_region(RegionName=region["RegionName"])
25 changes: 12 additions & 13 deletions upload-ami/src/upload_ami/request_public_ami_quota_increase.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@


def get_public_ami_service_quota(servicequotas):
pages = servicequotas.get_paginator(
'list_service_quotas').paginate(ServiceCode="ec2")
for page in pages:
for quota in page["Quotas"]:
if quota["QuotaName"] == "Public AMIs":
return quota
raise Exception("No public AMI quota found")
return next(servicequotas
.get_paginator('list_service_quotas')
.paginate(ServiceCode="ec2")
.search("Quotas[?QuotaName=='Public AMIs']"))


def main():
Expand All @@ -21,12 +18,14 @@ def main():
"service-quotas", region_name=region["RegionName"])
service_quota = get_public_ami_service_quota(servicequotas)
try:
logging.info(f"Requesting quota increase for {region['RegionName']}")
servicequotas.request_service_quota_increase(
ServiceCode="ec2",
QuotaCode=service_quota['QuotaCode'],
DesiredValue=100,
)
desired_value = 100
if service_quota['Value'] >= desired_value:
logging.info(
f"Quota for {region['RegionName']} is already {service_quota['Value']}")
continue
logging.info(
f"Requesting quota increase for {region['RegionName']}")
servicequotas.request_service_quota_increase( ServiceCode="ec2", QuotaCode=service_quota['QuotaCode'], DesiredValue=100,)
except Exception as e:
logging.warn(e)

Expand Down

0 comments on commit a9f28b5

Please sign in to comment.