2

我正在尝试通过 Shell 脚本对类似于 crontab 编辑的命令自动进行 VI 编辑,但到目前为止对我不起作用。

这是 admin 为 true 的最终 json:

'{"name":"SQLSRVR","admin":"true","json_class":"Chef::ApiClient","chef_type":"client"}'

如您所见,EDITOR 环境变量必须设置或作为命令行选项 -e 传递

[root@vrhost user]# knife client edit SQLSRVR
ERROR: RuntimeError: Please set EDITOR environment variable

[root@vrhost user]# knife client edit
USAGE: knife client edit CLIENT (options)
-s, --server-url URL             Chef Server URL
-k, --key KEY                    API Client Key
    --[no-]color                 Use colored output, defaults to enabled
-c, --config CONFIG              The configuration file to use
    --defaults                   Accept default values for all questions
-d, --disable-editing            Do not open EDITOR, just accept the data as is
-e, --editor EDITOR              Set the editor to use for interactive commands
-E, --environment ENVIRONMENT    Set the Chef environment
-F, --format FORMAT              Which format to use for output
-u, --user USER                  API Client Username
    --print-after                Show the data after a destructive operation
-V, --verbose                    More verbose output. Use twice for max verbosity
-v, --version                    Show chef version
-y, --yes                        Say yes to all prompts for confirmation
-h, --help                       Show this message
FATAL: You must specify a client name

以下命令打开 vim 编辑器进行编辑,以将 ["admin": "false"] 更改为 ["admin": "true"]:

[root@vrhost user]# knife client edit SQLSRVR -e vim 

{
  "name": "SQLSRVR",
  "admin": false,
  "json_class": "Chef::ApiClient",
  "chef_type": "client",
}

我试图通过一个 shell 脚本来做到这一点,并想自动化它并尝试了很多选项,但到目前为止还没有运气。

[root@vrhost ~]# (echo ^[:g/false/s/false/true/^[:wq!^M) | knife client edit SQLSRVR -e vim
Vim: Warning: Input is not from a terminal
Object unchanged, not saving

或者

[root@vrhost user]# echo (^[echo     '{"name":"SQLSRVR","admin":"true","json_class":"Chef::ApiClient","chef_type":"client"}'^[:w    q!^M) | knife client edit SQLSRVR -e

[root@vrhost ~]# knife client show SQLSRVR
admin:       false
chef_type:   client
json_class:  Chef::ApiClient
name:        SQLSRVR

这与通过 shell 脚本自动编辑 crontab 非常相似,但这对我不起作用。

4

4 回答 4

1

除非您真的需要特殊的 Vim 功能,否则您最好使用非交互式工具,例如sed,awk或 Perl / Python / Ruby /你最喜欢的脚本语言

也就是说,您可以使用静默批处理模式以非交互方式使用 Vim 。

vim -T dumb --noplugin -n -es -S "commands.ex" "filespec"

除了通过外部脚本从 via 中读取命令外-S "commands.ex",您还可以直接通过 via 给出一些命令-c cmd1 -c cmd2。有关:help -s-ex更多信息,请参阅。

于 2013-02-18T17:27:49.300 回答
0

查看

$ knife client edit --help
[...]
-d, --disable-editing            Do not open EDITOR, just accept the data as is

所以我猜你可以改变值而不用在 vim 中编辑。只是:

  • 获取json格式的客户端数据。
  • 用 sed 替换所需的值。
  • 从文件上传数据。

代码:

$ knife client show -Fj SQLSRVR > SQLSRVR.json
$ sed -i.old "s/\"admin\": true,/\"admin\": false,/" SQLSRVR.json
$ knife client edit -d SQLSRVR < SQLSRVR.json

类似的东西。

于 2013-02-20T09:05:01.007 回答
0

Here are some links to references:

i) http://mirror.hep.wisc.edu/stable/chef/chef-server-webui/app/controllers/clients_controller.rb

ii) http://www.rubydoc.info/github/opscode/chef/master/Shell/Extensions - tried but unable to get it to work

Finally did the following (it does give 409 the 2nd time on call and I did not need to do it a 2nd time):

# call to below rb, CLIENTNAME is the name of the client and STATE is true/false
$ knife exec clienttransform.rb CLIENTNAME STATE

$ cat clienttransform.rb

Chef::Config[:solo] = false

class Company
  class TransformClient

    attr_accessor :clientname
    attr_accessor :isclientadmin

    def initialize(client_name, is_client_admin)
      @clientname = client_name
      @isclientadmin = is_client_admin
    end

    def transform
      client=Chef::ApiClient.load(@clientname)
      # puts "client.name : " + client.name
      # puts "client.admin : " + client.admin.to_s
      # puts "XX - clientname : " + @clientname
      # puts "XX - isclientadmin : " + @isclientadmin.to_s
      boolisclientadmin = !!@isclientadmin
      client.admin(boolisclientadmin)
      client.save()
    end

  end

end

client_name = ARGV[2].to_s()
is_client_admin = ARGV[3].to_s()

# puts "YY - client_name : " + client_name
# puts "YY - is_client_admin : " + is_client_admin

trc = Company::TransformClient.new(client_name, is_client_admin)
trc.transform

exit 0
于 2013-02-24T19:12:15.127 回答
0

只需设置您的编辑器,它就会工作。就我而言,我使用 vim 编辑器,这就是为什么我的命令如下:

export EDITOR=vim
于 2013-03-25T09:38:36.167 回答