!/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;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&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. spring boot 1.4默认使用 hibernate validator

    spring boot 1.4默认使用 hibernate validator 5.2.4 Final实现校验功能.hibernate validator 5.2.4 Final是JSR 349 Be ...

  2. 关于为什么java需要垃圾回收

    为什么java采用垃圾回收而c++却不采用,这是因为在java中,所有对象变量都是引用,当一个引用被新对象覆盖掉时,就没有引用指向原来的对象了,这个对象就“失控了”. 而C++中,除非使用特殊符号&a ...

  3. Mybatis中配置Mapper的方法

    在这篇文章中我主要想讲一下Mybatis配置文件中mappers元素的配置.关于基础部分的内容可以参考http://haohaoxuexi.iteye.com/blog/1333271. 我们知道在M ...

  4. Android上常见度量单位【xdpi、hdpi、mdpi、ldpi】解读

    术语和概念  屏幕尺寸  屏幕的物理尺寸,以屏幕的对角线长度作为依据(比如 2.8寸, 3.5寸).  简而言之, Android把所有的屏幕尺寸简化为三大类:大,正常,和小.  程序可以针对这三种尺 ...

  5. Android开发之消息机制

    转:http://stackvoid.com/introduction-to-Message-Handler-in-Android/ http://blog.dreamtobe.cn/2016/03/ ...

  6. 进程间通信机制<转>

    1   文件映射 文件映射(Memory-Mapped Files)能使进程把文件内容当作进程地址区间一块内存那样来对待.因此,进程不必使用文件I/O操作,只需简单的指针操作就可读取和修改文件的内容. ...

  7. Android远程图片获取和本地缓存

    对于客户端——服务器端应用,从远程获取图片算是经常要用的一个功能,而图片资源往往会消耗比较大的流量,对 应用来说,如果处理不好这个问题,那会让用户很崩溃,不知不觉手机流量就用完了,等用户发现是你的应用 ...

  8. 可视化PK纯代码

    简述 其实今天说的内容不仅仅局限于Qt,在很多其它语言或者框架中也适用,那就是 - 用可视化工具or文本编辑器?拖or不拖? 如果有人问我喜欢脱or不脱?我会毫不犹豫地说不脱,因为我比较矜持O(∩_∩ ...

  9. 定义 androidlistview 滚动条位置

    1.找到每一页的最后一条数据的位置 public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, ...

  10. IOS cocos2d笔记1

    结点添加.删除.获取1.结点:CCNode * childNode = [CCNode node]; 2.加入结点[myNode addChild:childNode z:0 tag:123];//z ...