对于细心的读者可能已经注意到在我的先前的文章中"如何在Snappy Ubuntu Core中设置autopilot"已经体会到使用Snappy Ubuntu中的config的便利.我们可以来重新温顾一下这个命令的语法:
$ snappy config -h Usage: snappy [OPTIONS] config [package name] [config file] Configures a package. The configuration is a YAML file, provided in the specified file which can be "-" for stdin. Output of the command is the current configuration, so running this command with no input file provides a snapshot of the app's current config. Help Options: -h, --help Show this help message [config command arguments] package name: Set configuration for a specific installed package config file: The configuration for the given file
$ git clone https://github.com/liu-xiao-guo/config-example
在参阅本文章时,一定要注意一个bug.当我们使用ssh的方法进入到我们的target时,需要使用如下的方式.否则,我们的设置可能会发生错误.
$ LC_ALL=C.UTF-8 LANGUAGE=en ssh -p 8022 ubuntu@localhost
下面在我们展示在KVM中的运行情况:
(amd64)ubuntu@localhost:~$ snappy list -v Name Date Version Developer ubuntu-core 2015-11-13 10 ubuntu* ubuntu-core 2015-10-23 9 ubuntu config-example 2015-11-27 IGUIWdTOWeCa sideload* hello-xiaoguo 2015-11-05 IEeNEfBRVYGe sideload hello-xiaoguo 2015-11-27 IGTcTdAICYOL sideload* mqtt-piglow 2015-11-23 IGJVDbJOQafg sideload mqtt-piglow 2015-11-23 IGJVJYLbWCaC sideload* pastebinit 2015-11-02 1.4.0.0.2 mvo* webcam-webui 2015-11-27 IGTMIAMeQcbW sideload* webcam 2015-11-14 IFUaQLgTUKSU sideload* webdm 2015-10-23 0.9.2 canonical* generic-amd64 2015-10-23 1.4 canonical*
(amd64)ubuntu@localhost:~$ config-example.hello config: config-example: msg: good
它显示了当前的应用的配置文件.该文件存在于如下的地址:
(amd64)ubuntu@localhost:~$ cd /var/lib/apps/config-example.sideload/current (amd64)ubuntu@localhost:/var/lib/apps/config-example.sideload/current$ cat message config: config-example: msg: good
我们的"hello"脚本(在bin目录下的脚本)应用是这么写的:
#!/bin/sh if [ -e ${SNAP_APP_DATA_PATH}/message ]; then cat ${SNAP_APP_DATA_PATH}//message else cat <<EOF2 Hello from the config example! You can configure this message by running: $ cat > cfg.yaml <<'EOF' config: config-example: msg: | Yay! EOF $ sudo snappy config config-example cfg.yaml The message you set there is then displayed when running: $ config-example.hello EOF2 fi
(amd64)ubuntu@localhost:~$ snappy config config-example config: config-example: msg: good
(amd64)ubuntu@localhost:~$ snappy config config-example > my.yaml (amd64)ubuntu@localhost:~$ vi my.yaml我们通过修改my.yaml文件,并保存修改后的文件.然后使用如下的命令来重新设置我们的应用(在这里,我们把内容修改为" this is something very cool"):
(amd64)ubuntu@localhost:~$ sudo snappy config config-example my.yaml config: config-example: msg: this is something very cool (amd64)ubuntu@localhost:~$ snappy config config-example config: config-example: msg: this is something very cool
(amd64)ubuntu@localhost:/var/lib/apps/config-example.sideload/current$ cat message config: config-example: msg: this is something very cool
#!/usr/bin/env python3 import os import os.path import sys import yaml _CONFIG = 'message' _DEFAULT_MESSAGE = 'Hello, the world' def main(): config_file = os.path.join(os.environ['SNAP_APP_DATA_PATH'], _CONFIG) config_yaml = yaml.load(sys.stdin) if config_yaml: set_config(config_file, config_yaml) yaml.dump(get_config(config_file), stream=sys.stdout, default_flow_style=False) def set_config(config_file, config_yaml={}): with open(config_file, 'w') as f: yaml.dump(_config(config_yaml), stream=f, default_flow_style=False) return config_yaml def get_config(config_file): try: with open(config_file) as f: return yaml.load(f) except FileNotFoundError: return _config() def _config(config_yaml={}): try: msg_value = config_yaml['config'][ os.environ['SNAP_NAME']]['msg'] if not isinstance( msg_value, str): config_yaml['config'][ os.environ['SNAP_NAME']]['msg'] = _DEFAULT_MESSAGE except KeyError: msg = { 'config': { os.environ['SNAP_NAME']: { 'msg': _DEFAULT_MESSAGE } } } config_yaml.update(msg) return config_yaml if __name__ == '__main__': main()