diff --git a/README.md b/README.md new file mode 100644 index 0000000..1426325 --- /dev/null +++ b/README.md @@ -0,0 +1,98 @@ +# AsCode - The Real Infrastructure as Code + +**AsCode** is a tool for define infrastructure as code using the [Starlark](https://github.com/google/starlark-go/blob/master/doc/spec.md) language on top of [Terraform](https://github.com/hashicorp/terraform). It allows to describe your infrastructure using a turing complete language in Terraform without writing a single line of [HCL](https://www.terraform.io/docs/configuration/syntax.html), meanwhile, you have the complete ecosystem of [providers](https://www.terraform.io/docs/providers/index.html) + +### Why? + +Terraform is a great tool, with support for almost everything you can imagine, making it the industry leader. Terraform is based on HCL, a JSON-alike declarative language, with a very limited control flow functionalities. IMHO, to really unleash the power of the IaC, a turing complete language should be used, where basic elements like loops or functions are first class citizens. + + +### What is Starlark? + +> Starlark is a dialect of Python intended for use as a configuration language. A Starlark interpreter is typically embedded within a larger application, and this application may define additional domain-specific functions and data types beyond those provided by the core language. For example, Starlark is embedded within (and was originally developed for) the Bazel build tool, and Bazel's build language is based on Starlark. + +## Examples + +### Simple + +Creating am Amazon EC2 Instance is as easy as: + +```pyhon +aws = provider("aws", "2.13.0") +aws.region = "us-west-2" + +aws.resource.instance(instance_type ="t2.micro", type="ami-2757f631") +``` +### Using functions + +In this example we create 40 instances, 20 using ubuntu and 20 using ECS. + +```python +aws = provider("aws") +aws.region = "us-west-2" + +# It creates a new instance for the given name, distro and type. +def new_instance(name, distro, type="t2.micro"): + instance = aws.resource.instance(name) + instance.instance_type = type + instance.ami = get_ami_id(distro) + + return instance + +amis = {} +ami_names_owners = { + "ubuntu": ["ubuntu/images/*/ubuntu-xenial-16.04-amd64-server-*", "099720109477"], + "ecs": ["*amazon-ecs-optimized", "591542846629"], +} + +# We create the AMI data-source for the given distro. +def get_ami_id(distro): + if distro in amis: + return amis[distro] + + data = ami_names_owners[distro] + + ami = aws.data.ami(distro) + ami.most_recent = True + ami.filter(name="name", values=[data[0]]) + ami.filter(name="virtualization-type", values=["hvm"]) + ami.owners = [data[1]] + + amis[distro] = ami.id + return ami.id + +# Creates 20 instances of each distro. +for i in range(20): + new_instance("ubuntu_%d" % i, "ubuntu") + new_instance("ecs_%d" % i, "ecs") + ``` + +### Using the runtime + +ascode comes with a built-in runtime with functions to work with `yaml`, `json`, `http`, etc. Take a look to the [documentation](/_documentation/runtime). + +``` +load("encoding/base64", "base64") +load("http", "http") + +dec = base64.encode("ascode is amazing") + +msg = http.get("https://httpbin.org/base64/%s" % dec) +print(msg.body()) +``` + + +## Installation + +The recommended way to install *ascode* is: + +``` +GO111MODULE=on go get -u github.com/mcuadros/ascode/... +``` + +Or you can download the binary from the [releases](https://github.com/mcuadros/ascode/releases) section. + + +## License + +GPL-3.0, see [LICENSE](LICENSE) \ No newline at end of file diff --git a/_examples/aws.star b/_examples/aws.star new file mode 100644 index 0000000..5fdab34 --- /dev/null +++ b/_examples/aws.star @@ -0,0 +1,37 @@ +aws = provider("aws") +aws.region = "us-west-2" + +# It creates a new instance for the given name, distro and type. +def new_instance(name, distro, type="t2.micro"): + instance = aws.resource.instance(name) + instance.instance_type = type + instance.ami = get_ami_id(distro) + + return instance + +amis = {} +ami_names_owners = { + "ubuntu": ["ubuntu/images/*/ubuntu-xenial-16.04-amd64-server-*", "099720109477"], + "ecs": ["*amazon-ecs-optimized", "591542846629"], +} + +# We create the AMI data-source for the given distro. +def get_ami_id(distro): + if distro in amis: + return amis[distro] + + data = ami_names_owners[distro] + + ami = aws.data.ami(distro) + ami.most_recent = True + ami.filter(name="name", values=[data[0]]) + ami.filter(name="virtualization-type", values=["hvm"]) + ami.owners = [data[1]] + + amis[distro] = ami.id + return ami.id + +# Creates 20 instances of each distro. +for i in range(20): + new_instance("ubuntu_%d" % i, "ubuntu") + new_instance("ecs_%d" % i, "ecs") \ No newline at end of file diff --git a/examples/ignition.star b/_examples/ignition.star similarity index 63% rename from examples/ignition.star rename to _examples/ignition.star index 1f515ff..d51e95c 100644 --- a/examples/ignition.star +++ b/_examples/ignition.star @@ -11,10 +11,12 @@ disk.device = "/dev/sda" root = disk.partition() root.start = 2048 -root.size = 4 * 1024 * 1024 +root.size = 4 * 1024 * 1024 home = disk.partition() -home.start = root.size + root.start -home.size = 4 * 1024 * 1024 +home.start = root.size + root.start +home.size = 4 * 1024 * 1024 + +ignition.data.config(disks=[disk.id], users=[user.id]) + -config = ignition.data.config(disks=[disk.id], users=[user.id]) \ No newline at end of file diff --git a/_examples/runtime.star b/_examples/runtime.star new file mode 100644 index 0000000..5afc154 --- /dev/null +++ b/_examples/runtime.star @@ -0,0 +1,7 @@ +load("encoding/base64", "base64") +load("http", "http") + +dec = base64.encode("ascode is amazing") + +msg = http.get("https://httpbin.org/base64/%s" % dec) +print(msg.body()) \ No newline at end of file diff --git a/examples/aws.star b/examples/aws.star deleted file mode 100644 index 40d5e5a..0000000 --- a/examples/aws.star +++ /dev/null @@ -1,35 +0,0 @@ -aws = provider("aws") -print(dir(aws)) - -ami = aws.data.ami() -ami.most_recent = True -ami.filter(name="name", values=["ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*"]) -ami.filter(name="virtualization-type", values=["hvm"]) -print(ami.filter[0], ami.filter[1]) -ami.filter[0].values = [] - -ami.owners = ["099720109477"] -print(ami.__dict__) - - -web = aws.resource.instance(ami=ami.id, instance_type="t2.micro") - -template = aws.resource.launch_template() -template.name_prefix = "example" -template.instance_type = "c5.larger" - -group = aws.resource.autoscaling_group() -group.availability_zones = ["us-east-1a"] -group.desired_capacity = 1 -group.max_size = 1 -group.min_size = 1 - -group.mixed_instances_policy = { - "launch_template": { - "launch_template_specification": { - "launch_template_id": "bar" - } - } -} - -print(group.__dict__) \ No newline at end of file diff --git a/examples/encoding.star b/examples/encoding.star deleted file mode 100644 index a0d182e..0000000 --- a/examples/encoding.star +++ /dev/null @@ -1,17 +0,0 @@ -load("encoding/json", "json") -load("encoding/base64", "base64") -load("os", "os") -load("path/filepath", "filepath") - -ignition = provider("ignition", "1.1.0") - -user = ignition.data.user() -user.name = "foo" -user.uid = 42 -user.groups = ["foo", "bar"] -user.system = True - -print(json.dumps(user.__dict__)) -print(base64.encode("foo")) - -print(filepath.base(os.getwd())) \ No newline at end of file diff --git a/starlark/runtime/runtime.go b/starlark/runtime/runtime.go index dc70301..2829215 100644 --- a/starlark/runtime/runtime.go +++ b/starlark/runtime/runtime.go @@ -19,7 +19,9 @@ import ( ) func init() { + resolve.AllowRecursion = true resolve.AllowFloat = true + resolve.AllowGlobalReassign = true } type LoadModuleFunc func() (starlark.StringDict, error)