Skip to content

Commit d3d153b

Browse files
committed
examples and documentation
1 parent 10f8943 commit d3d153b

File tree

7 files changed

+150
-56
lines changed

7 files changed

+150
-56
lines changed

README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# AsCode - The Real Infrastructure as Code
2+
3+
**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)
4+
5+
### Why?
6+
7+
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.
8+
9+
10+
### What is Starlark?
11+
12+
> 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.
13+
14+
## Examples
15+
16+
### Simple
17+
18+
Creating am Amazon EC2 Instance is as easy as:
19+
20+
```pyhon
21+
aws = provider("aws", "2.13.0")
22+
aws.region = "us-west-2"
23+
24+
aws.resource.instance(instance_type ="t2.micro", type="ami-2757f631")
25+
```
26+
### Using functions
27+
28+
In this example we create 40 instances, 20 using ubuntu and 20 using ECS.
29+
30+
```python
31+
aws = provider("aws")
32+
aws.region = "us-west-2"
33+
34+
# It creates a new instance for the given name, distro and type.
35+
def new_instance(name, distro, type="t2.micro"):
36+
instance = aws.resource.instance(name)
37+
instance.instance_type = type
38+
instance.ami = get_ami_id(distro)
39+
40+
return instance
41+
42+
amis = {}
43+
ami_names_owners = {
44+
"ubuntu": ["ubuntu/images/*/ubuntu-xenial-16.04-amd64-server-*", "099720109477"],
45+
"ecs": ["*amazon-ecs-optimized", "591542846629"],
46+
}
47+
48+
# We create the AMI data-source for the given distro.
49+
def get_ami_id(distro):
50+
if distro in amis:
51+
return amis[distro]
52+
53+
data = ami_names_owners[distro]
54+
55+
ami = aws.data.ami(distro)
56+
ami.most_recent = True
57+
ami.filter(name="name", values=[data[0]])
58+
ami.filter(name="virtualization-type", values=["hvm"])
59+
ami.owners = [data[1]]
60+
61+
amis[distro] = ami.id
62+
return ami.id
63+
64+
# Creates 20 instances of each distro.
65+
for i in range(20):
66+
new_instance("ubuntu_%d" % i, "ubuntu")
67+
new_instance("ecs_%d" % i, "ecs")
68+
```
69+
70+
### Using the runtime
71+
72+
ascode comes with a built-in runtime with functions to work with `yaml`, `json`, `http`, etc. Take a look to the [documentation](/_documentation/runtime).
73+
74+
```
75+
load("encoding/base64", "base64")
76+
load("http", "http")
77+
78+
dec = base64.encode("ascode is amazing")
79+
80+
msg = http.get("https://httpbin.org/base64/%s" % dec)
81+
print(msg.body())
82+
```
83+
84+
85+
## Installation
86+
87+
The recommended way to install *ascode* is:
88+
89+
```
90+
GO111MODULE=on go get -u github.com/mcuadros/ascode/...
91+
```
92+
93+
Or you can download the binary from the [releases](https://github.com/mcuadros/ascode/releases) section.
94+
95+
96+
## License
97+
98+
GPL-3.0, see [LICENSE](LICENSE)

_examples/aws.star

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
aws = provider("aws")
2+
aws.region = "us-west-2"
3+
4+
# It creates a new instance for the given name, distro and type.
5+
def new_instance(name, distro, type="t2.micro"):
6+
instance = aws.resource.instance(name)
7+
instance.instance_type = type
8+
instance.ami = get_ami_id(distro)
9+
10+
return instance
11+
12+
amis = {}
13+
ami_names_owners = {
14+
"ubuntu": ["ubuntu/images/*/ubuntu-xenial-16.04-amd64-server-*", "099720109477"],
15+
"ecs": ["*amazon-ecs-optimized", "591542846629"],
16+
}
17+
18+
# We create the AMI data-source for the given distro.
19+
def get_ami_id(distro):
20+
if distro in amis:
21+
return amis[distro]
22+
23+
data = ami_names_owners[distro]
24+
25+
ami = aws.data.ami(distro)
26+
ami.most_recent = True
27+
ami.filter(name="name", values=[data[0]])
28+
ami.filter(name="virtualization-type", values=["hvm"])
29+
ami.owners = [data[1]]
30+
31+
amis[distro] = ami.id
32+
return ami.id
33+
34+
# Creates 20 instances of each distro.
35+
for i in range(20):
36+
new_instance("ubuntu_%d" % i, "ubuntu")
37+
new_instance("ecs_%d" % i, "ecs")

examples/ignition.star renamed to _examples/ignition.star

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ disk.device = "/dev/sda"
1111

1212
root = disk.partition()
1313
root.start = 2048
14-
root.size = 4 * 1024 * 1024
14+
root.size = 4 * 1024 * 1024
1515

1616
home = disk.partition()
17-
home.start = root.size + root.start
18-
home.size = 4 * 1024 * 1024
17+
home.start = root.size + root.start
18+
home.size = 4 * 1024 * 1024
19+
20+
ignition.data.config(disks=[disk.id], users=[user.id])
21+
1922

20-
config = ignition.data.config(disks=[disk.id], users=[user.id])

_examples/runtime.star

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
load("encoding/base64", "base64")
2+
load("http", "http")
3+
4+
dec = base64.encode("ascode is amazing")
5+
6+
msg = http.get("https://httpbin.org/base64/%s" % dec)
7+
print(msg.body())

examples/aws.star

Lines changed: 0 additions & 35 deletions
This file was deleted.

examples/encoding.star

Lines changed: 0 additions & 17 deletions
This file was deleted.

starlark/runtime/runtime.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ import (
1919
)
2020

2121
func init() {
22+
resolve.AllowRecursion = true
2223
resolve.AllowFloat = true
24+
resolve.AllowGlobalReassign = true
2325
}
2426

2527
type LoadModuleFunc func() (starlark.StringDict, error)

0 commit comments

Comments
 (0)