Skip to content

Commit

Permalink
new file and command component usage in examples and tutorials section
Browse files Browse the repository at this point in the history
Ticket: ENT-10940
Changelog: None
Signed-off-by: Mikita Pilinka <[email protected]>
Merge pull request cfengine#3126 from nickanderson/docker-fixups

Cleaned up package from doc artifacts
  • Loading branch information
mineralsfree committed Jan 10, 2024
1 parent 18b4d66 commit 32e6009
Show file tree
Hide file tree
Showing 42 changed files with 590 additions and 394 deletions.
30 changes: 18 additions & 12 deletions examples.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Following these steps, you will login to your policy server via the SSH protocol
5. In the vi editor, enter ```i``` for "Insert" and enter the following content (ie. copy and paste from a text editor):

```cf3
[file=hello_world.cf]
bundle agent hello_world
{
reports:
Expand Down Expand Up @@ -78,21 +79,23 @@ hosts][Classes and decisions].

Activate the bundle manually by executing the following command at prompt:

```console
```command
/var/cfengine/bin/cf-agent --no-lock --file ./hello_world.cf --bundlesequence hello_world
```

This command instructs CFEngine to ignore [locks][Controlling frequency], load
the `hello_world.cf` policy, and activate the `hello_world` bundle. See the output below:

```console
# /var/cfengine/bin/cf-agent --no-lock --file ./hello_world.cf --bundlesequence hello_world
```command
/var/cfengine/bin/cf-agent --no-lock --file ./hello_world.cf --bundlesequence hello_world
```
```output
2013-08-20T14:03:43-0500 notice: R: Hello World!
```

As you get familiar with CFEngine, you'll probably start shortening this command to this equivalent:

```console
```command
/var/cfengine/bin/cf-agent -Kf ./hello_world.cf -b hello_world
```

Expand All @@ -118,6 +121,7 @@ Go back into vi by typing "vi" at the prompt. Then type ```i``` to insert
shown in the following example:

```cf3
[file=hello_world.cf]
body common control
{
bundlesequence => { "hello_world" };
Expand All @@ -138,20 +142,23 @@ Now press "esc" to exit the "Insert" mode, then type ```:w``` to save the file c
Exit vi by typing ```:q``` then "Enter." This will return to the prompt.

Execute the following command:
```console
```command
/var/cfengine/bin/cf-agent --no-lock --file ./hello_world.cf
```

The output is shown below:

```console
# /var/cfengine/bin/cf-agent --no-lock --file ./hello_world.cf
```command
/var/cfengine/bin/cf-agent --no-lock --file ./hello_world.cf
```
```output
2013-08-20T14:25:36-0500 notice: R: Hello World!
```

Note: It may be necessary to add a reference to the standard library within the body common control section, and remove the bundlesequence line. Example:

```cf3
[file=hello_world.cf]
body common control {
inputs => {
"libraries/cfengine_stdlib.cf",
Expand All @@ -171,6 +178,7 @@ Again type "vi" then "Enter" then ```i``` to insert the following:
Add it before __body common control__, as shown below:

```cf3
[file=hello_world.cf]
#!/var/cfengine/bin/cf-agent --no-lock
body common control
{
Expand All @@ -193,21 +201,19 @@ then exit vi by typing ```:q``` then "Enter". This will return to the prompt.

Make the policy file executable, and then run it, by typing the following two commands:

```console
```command
chmod +x ./hello_world.cf
```

Followed by:

```console
```command
./hello_world.cf
```

See the output below:

```console
# chmod +x ./hello_world.cf
# ./hello_world.cf
```output
2013-08-20T14:39:34-0500 notice: R: Hello World!
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@ If any of these classes becomes defined, it will cause the current bundle to be
[%CFEngine_include_example(abort.cf)%]

This is how the policy runs when the userlist is valid:

# cf-agent -f unit_abort.cf
R: User name mark is valid at 4 letters
R: User name john is valid at 4 letters
#

```command
cf-agent -f unit_abort.cf
```
```output
R: User name mark is valid at 4 letters
R: User name john is valid at 4 letters
```
This is how the policy runs when the userlist contains an invalid entry:

# cf-agent -f unit_abort.cf
Bundle example aborted on defined class "invalid"
#

```command
cf-agent -f unit_abort.cf
```
```output
Bundle example aborted on defined class "invalid"
```
To run this example file as part of your main policy you need to make an
additional change:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,21 @@ First, let's create some files for CFEngine to monitor:

CFEngine detects new files and adds them to the file integrity database:

```command
cf-agent -f unit_change_detect.cf
```
# cf-agent -f unit_change_detect.cf
```output
2013-06-06T20:53:26-0700 error: /example/files/'/etc/example':
File '/etc/example/example.conf' was not in 'md5' database - new file found
# cf-agent -f unit_change_detect.cf -K
```
```command
cf-agent -f unit_change_detect.cf -K
```

If there are no changes, CFEngine runs silently:

```
# cf-agent -f unit_change_detect.cf
#
```command
cf-agent -f unit_change_detect.cf
```

Now let's update the mtime, and then the mtime and content.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ and set permissions on both.

Example output:

```command
cf-agent -f unit_create_filedir.cf -I
```
# cf-agent -f unit_create_filedir.cf -I
```output
2013-06-08T14:56:26-0700 info: /example/files/'/home/mark/tmp/test_plain': Created file '/home/mark/tmp/test_plain', mode 0640
2013-06-08T14:56:26-0700 info: /example/files/'/home/mark/tmp/test_dir/.': Created directory '/home/mark/tmp/test_dir/.'
2013-06-08T14:56:26-0700 info: /example/files/'/home/mark/tmp/test_dir/.': Object '/home/mark/tmp/test_dir' had permission 0755, changed it to 0750
#
```
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ Check how much space (in KB) is available on a directory's current partition.

Example output:

```command
cf-agent -f unit_diskfree.cf
```
# cf-agent -f unit_diskfree.cf
```output
R: Freedisk 48694692
# df -k /tmp
```
```command
df -k /tmp
```
```output
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 149911836 93602068 48694692 66% /
#
```
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ The bundle is defined like this:

**Example run:**

```console
root@debian8:~/core/examples# cf-agent -KIf ./mustache_template_motd.cf; cat /etc/motd
```command
cf-agent -KIf ./mustache_template_motd.cf; cat /etc/motd
```
```output
info: Updated rendering of '/etc/motd' from mustache template 'inline'
info: files promise '/etc/motd' repaired
# Managed by CFEngine
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ correct return codes for status checks.
We can see that before the policy run `sysstat` is *inactive*, `apache2` is
*active*, `cups` is *active*, `ssh` is *active* and `cron` is *inactive*.

```console
root@ubuntu:# systemctl is-active sysstat apache2 cups ssh cron
```command
systemctl is-active sysstat apache2 cups ssh cron
```
```output
inactive
active
active
Expand All @@ -35,21 +37,25 @@ inactive

Now we run the policy to converge the system to the desired state.

```console
root@ubuntu:# cf-agent --no-lock --inform --file ./services.cf
info: Executing 'no timeout' ... '/bin/systemctl --no-ask-password --global --system -q stop apache2'
info: Completed execution of '/bin/systemctl --no-ask-password --global --system -q stop apache2'
info: Executing 'no timeout' ... '/bin/systemctl --no-ask-password --global --system -q stop cups'
info: Completed execution of '/bin/systemctl --no-ask-password --global --system -q stop cups'
info: Executing 'no timeout' ... '/bin/systemctl --no-ask-password --global --system -q start cron'
info: Completed execution of '/bin/systemctl --no-ask-password --global --system -q start cron'
```command
cf-agent --no-lock --inform --file ./services.cf
```
```output
info: Executing 'no timeout' ... '/bin/systemctl --no-ask-password --global --system -q stop apache2'
info: Completed execution of '/bin/systemctl --no-ask-password --global --system -q stop apache2'
info: Executing 'no timeout' ... '/bin/systemctl --no-ask-password --global --system -q stop cups'
info: Completed execution of '/bin/systemctl --no-ask-password --global --system -q stop cups'
info: Executing 'no timeout' ... '/bin/systemctl --no-ask-password --global --system -q start cron'
info: Completed execution of '/bin/systemctl --no-ask-password --global --system -q start cron'
```

After the policy run we can see that `systat`, `apache2`, and `cups` are
*inactive*. `ssh` and `cron` are *active* as specified in the policy.

```console
root@ubuntu:/home/nickanderson/CFEngine/core/examples# systemctl is-active sysstat apache2 cups ssh cron
```command
systemctl is-active sysstat apache2 cups ssh cron
```
```output
inactive
inactive
inactive
Expand All @@ -63,52 +69,90 @@ We can see that before the policy run `sysstat` is not reporting status
correctly , `httpd` is *running*, `cups` is *running*, `sshd` is *running* and
`crond` is *not running*.

```console
[root@localhost examples]# service sysstat status; echo $?
```command
service sysstat status; echo $?
```
```output
3
[root@localhost examples]# service httpd status; echo $?
```
```command
service httpd status; echo $?
```
```output
httpd (pid 3740) is running...
0
[root@localhost examples]# service cups status; echo $?
```
```command
service cups status; echo $?
```
```output
cupsd (pid 3762) is running...
0
[root@localhost examples]# service sshd status; echo $?
```
```command
service sshd status; echo $?
```
```output
openssh-daemon (pid 3794) is running...
0
[root@localhost examples]# service crond status; echo $?
```
```command
service crond status; echo $?
```
```output
crond is stopped
3
```

Now we run the policy to converge the system to the desired state.

```console
[root@localhost examples]# cf-agent -KIf ./services.cf
info: Executing 'no timeout' ... '/etc/init.d/crond start'
info: Completed execution of '/etc/init.d/crond start'
info: Executing 'no timeout' ... '/etc/init.d/httpd stop'
info: Completed execution of '/etc/init.d/httpd stop'
info: Executing 'no timeout' ... '/etc/init.d/cups stop'
info: Completed execution of '/etc/init.d/cups stop'
```command
cf-agent -KIf ./services.cf
```
```output
info: Executing 'no timeout' ... '/etc/init.d/crond start'
info: Completed execution of '/etc/init.d/crond start'
info: Executing 'no timeout' ... '/etc/init.d/httpd stop'
info: Completed execution of '/etc/init.d/httpd stop'
info: Executing 'no timeout' ... '/etc/init.d/cups stop'
info: Completed execution of '/etc/init.d/cups stop'
```

After the policy run we can see that `systat` is still not reporting status correctly (some services do not respond to standard checks), `apache2`, and `cups` are
*inactive*. `ssh` and `cron` are *active* as specified in the policy.


```console
[root@localhost examples]# service sysstat status; echo $?
```command
service sysstat status; echo $?
```
```output
3
[root@localhost examples]# service httpd status; echo $?
```
```command
service httpd status; echo $?
```
```output
httpd is stopped
3
[root@localhost examples]# service cups status; echo $?
cupsd is stopped
```
```command
service cups status; echo $?
```
```output
cups is stopped
3
[root@localhost examples]# service sshd status; echo $?
```
```command
service sshd status; echo $?
```
```output
openssh-daemon (pid 3794) is running...
0
[root@localhost examples]# service crond status; echo $?
```
```command
service crond status; echo $?
```
```output
crond (pid 3929) is running...
0
```
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@ This policy can be found in `/var/cfengine/masterfiles/example_find_mac_addr.cf`

Example run:

```command
cf-agent -f example_find_mac_addr.cf
```
# cf-agent -f example_find_mac_addr.cf
```output
2013-06-08T16:59:19-0700 notice: R: MAC address is a4:ba:db:d7:59:32
#
```

While the above illustrates the flexiblity of CFEngine in
Expand Down
Loading

0 comments on commit 32e6009

Please sign in to comment.