!/usr/bin/env bash

 

_chef_dir ()
{

# Helper function:

# Recursive function that searches for chef configuration directory

# It looks upward from the cwd until it hits /. If no directory is found,

# ~/.chef is chosen if it exists

# You could simply hard-code the path below

 

if
[
"$PWD"
=
"/"
]; then

if
[ -d ".chef"
]; then


echo
"/.chef"

elif
[ -d "$HOME/.chef"
]; then


echo
"$HOME/.chef"

fi

return

fi

 

if
[ -d '.chef'
];then


echo
"${PWD}/.chef"

else

(cd ..; _chef_dir)

fi

}

 

_chomp ()
{

# helper function to remove newlines

awk '{printf "%s", $0}'

}

 

chef_api_request()
{

# This is the meat-and-potatoes, or rice-and-vegetables, your preference really.

 

local method path body timestamp chef_server_url client_name hashed_body hashed_path

local canonical_request headers auth_headers

 

chef_server_url="https://api.opscode.com/organizations/my_org"

# '/organizations/ORG_NAME' is needed for Hosted Chef or Private Chef

if echo
$chef_server_url | grep -q "/organizations/" ; then


endpoint=/organizations/${chef_server_url#*/organizations/}${2%%\?*}

else


endpoint=${$2%%\?*}

fi


path=${chef_server_url}$2

client_name="chef_user"

method=$1

body=$3

 

hashed_path=$(echo -n "$endpoint" | openssl dgst -sha1 -binary | openssl enc -base64)

hashed_body=$(echo -n "$body" | openssl dgst -sha1 -binary | openssl enc -base64)

timestamp=$(date -u "+%Y-%m-%dT%H:%M:%SZ")

 

canonical_request="Method:$method\nHashed Path:$hashed_path\nX-Ops-Content-Hash:$hashed_body\nX-Ops-Timestamp:$timestamp\nX-Ops-UserId:$client_name"

headers="-H X-Ops-Timestamp:$timestamp \

-H X-Ops-Userid:$client_name \

-H X-Chef-Version:0.10.4 \

-H Accept:application/json \

-H X-Ops-Content-Hash:$hashed_body \

-H X-Ops-Sign:version=1.0"

 

auth_headers=$(printf
"$canonical_request" | openssl rsautl -sign -inkey \

"$(_chef_dir)/${client_name}.pem" | openssl enc -base64 | _chomp | awk '{ll=int(length/60);i=0; \

while (i<=ll) {printf " -H X-Ops-Authorization-%s:%s", i+1, substr($0,i*60+1,60);i=i+1}}')

 

case
$method in

GET)

curl_command="curl $headers $auth_headers $path"

$curl_command

;;

*)

echo
"Unknown Method. I only know: GET" >&2

return 1

;;

esac

}

 

chef_api_request "$@"

How to interact with the Chef Server using the Chef Server API using Shell script的更多相关文章

  1. Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds

    错误:Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds 错误提示就是我们限定了部署的时间导致的错 ...

  2. Step by step Install a Local Report Server and Remote Report Server Database

    原创地址:http://www.cnblogs.com/jfzhu/p/4012097.html 转载请注明出处 前面的文章<Step by step SQL Server 2012的安装 &g ...

  3. Server Tomcat v7.0 Server at localhost was unable to&amp;amp;nbs 报错问题解决

    在eclipse启动tomcat时遇到超时45秒的问题: Server Tomcat v7.0 Server at localhost was unable to start within 45 se ...

  4. Comparison of SQL Server Compact, SQLite, SQL Server Express and LocalDB

    Information about LocalDB comes from here and SQL Server 2014 Books Online. LocalDB is the full SQL ...

  5. Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds...

    仰天长啸   Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds... 当启动tomcat时候出现 ...

  6. Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds -----》myeclipse2015

    错误:Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds 错误提示就是我们限定了部署的时间导致的错 ...

  7. Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds问题

    错误:Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds. If the server requi ...

  8. Server Tomcat v7.0 Server at localhost was unable to&amp;nbs 报错问题解决

    在eclipse启动tomcat时遇到超时45秒的问题: Server Tomcat v7.0 Server at localhost was unable to start within 45 se ...

  9. Server Tomcat v7.0 Server at localhost failed to start.临时解决办法

    错误名:Server Tomcat v7.0 Server at localhost failed to start. 解决办法:去掉下面这句话: (通常在代码开头部分,public class前) ...

随机推荐

  1. PHP数组函数: array_walk()

    定义和用法 array_walk() 函数对数组中的每个元素应用回调函数.如果成功则返回 TRUE,否则返回 FALSE. 典型情况下 function 接受两个参数.array 参数的值作为第一个, ...

  2. 前端学习——css实用技术

    一,css控制文本样式 文本相关的css属性有很多,包括: color;font-size;font-weight;text-transform(大小写uppercase等);text-decorat ...

  3. new Option及用法

    op.sclassnumber.options.add(new  Option(secondMenuClassNumber[0]+":"+secondMenuText[0]),se ...

  4. predis如何实现phpredis的pconnect方法

    predis和phpredis都是redis的php客户端,区别可以看这里,这里不赘述. phpredis是php扩展,由C语言编写,诞生较早,很多PHPer都熟悉. predis是用PHP语言编写, ...

  5. AutoIt3初探(1)

    AutoIt3可实现系统操作,键盘鼠标模拟,是自动化测试的一个好工具. 这个是在线帮助文档,http://www.jb51.net/shouce/autoit/ 需要先下载一个autoIt安装,然后将 ...

  6. 魔法方法:构造和析构 - 零基础入门学习Python041

    魔法方法:构造和析构 让编程改变世界 Change the world by program 构造和析构 什么是魔法方法呢?我们来系统总结下: - 魔法方法总是被双下划线包围,例如__init__ - ...

  7. 高速压缩跟踪(fast compressive tracking)(CT)算法分析

    本文为原创,转载请注明出处:http://blog.csdn.net/autocyz/article/details/44490009 Fast Compressive Tracking (高速压缩跟 ...

  8. 机器学习:Python中如何使用支持向量机(SVM)算法

    (简单介绍一下支持向量机,详细介绍尤其是算法过程可以查阅其他资) 在机器学习领域,支持向量机SVM(Support Vector Machine)是一个有监督的学习模型,通常用来进行模式识别.分类(异 ...

  9. 自定义一个View作为弹出对话框

    public void dialog(){ //获得布局对象 View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialo ...

  10. 配置远程主机http服务器 打包资源

    <1> 搭建nginx  验证nginx是否启动成功 https://blog.csdn.net/wdsdsdsds/article/details/51179780 https://ww ...